Question: Not sure if I’m returning the Boolean correctly.
Current behavior: Will print out True even when the user input is false.
Desired behavior
Repl link:
code snippet
Question: Not sure if I’m returning the Boolean correctly.
Current behavior: Will print out True even when the user input is false.
Desired behavior
Repl link:
code snippet
Welcome to the community! Please include a code snippet and/or the link to your Repl as well as the language you’re using.
Is the Repl private? I’m getting a 404 error
How to make unprivate please?
Lets look at this snippet of your code:
if player1 == "R" or "r":
if player2 == "R" or "r":
print("Both played Rock, it is a draw!")
Here player1
and player2
are strings that have either “r”, “p”, “s”, “R”, “P”, “S”.
For this line:
if player1 == "R" or "r":
Lets say player1
was “P”. It is clearly not “R” but the program says that it IS R.
This is because of the way the or
is used.
You wanted to say that if player1
is “R” or if player1
is “r” then return True.
But the logical statement is framed incorrectly.
Try running
player1 = "P"
print(bool(player1 == "R" or "r")) #your condition
print(bool(player1 == "R" or player1 == "r")) #the one you meant to code
bool()
just converts the output to boolean (you dont have to use it in of loops anyway)
The output you would get is:
True
False
Reason:
The reason is operator precedence.
This is the order:
As you can see, or
is below ==
in the order.
So the expression is evaluated as:
if (player1 == "R") or "r"
if False or "r"
if "r"
if True
(truth value of any non empty sequence is True
)But for the correct one:
if (player1 == "R") or (player1 == "r")
if False and False
if False
So just change all the condition from
if x == "" or "":
to if x == "" or x == "":
I can understand where this mistake comes from though, you’d think it work when writing it the first time, but it does not, here’s some alternate ideas:
# One way
if a == "a" or a == "b":
# Code here
# Another way
if a in ["a","b"]:
#Code here
Hope this helps!
On another note, where did the person above me get this code from? The repl is still private.
Taking a guess based on the original post, you might have an input like this?
test = input("Provide a boolean: ")
Or something where they input true or false. If you want to convert this to a boolean, you could do this:
test = input("Provide a boolean: ")
if test.lower() == "true":
test = True
else:
test = False
Hope this helps!
On another note, where did the person above me get this code from? The repl is still private.
I’m the dreaded hacker nobody knows of
Lol i got it from the replit account @\princessEkoh