Returning a booelean

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.

1 Like

Is the Repl private? I’m getting a 404 error


How to make unprivate please?

Click the name of your Repl and click Public.


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:


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:

  1. if (player1 == "R") or "r"
  2. if False or "r"
  3. if "r"
  4. if True (truth value of any non empty sequence is True)

But for the correct one:

  1. if (player1 == "R") or (player1 == "r")
  2. if False and False
  3. if False
    This is the intended behavior.

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
  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 :smiling_imp:
Lol i got it from the replit account @\princessEkoh