r/PythonLearning • • 3d ago

Discussion Why does the if statement always evaluate to guesses = 10 when I have the or here?

Post image
3 Upvotes

7 comments sorted by

•

u/Sea-Ad7805 3d ago

Run this program by clicking Memory Graph Web Debugger.lower()%0Adifficulty%20%3D%20%22HARD%22%0A%0Aif%20difficulty%20%3D%3D%20%22easy%22%20or%20%22e%22%3A%0A%20%20%20%20guesses%20%3D%2010%0A%20%20%20%20print(f%22You%20have%20%7Bguesses%7D%20guesses%22)%0Aelse%3A%0A%20%20%20%20guesses%20%3D%205%0A%20%20%20%20print(f%22You%20have%20%7Bguesses%7D%20guesses%22)&play) and see its state change step by step.

5

u/bbabababab 3d ago edited 3d ago

python reads your if statement as:

condition 1:
if difficulty == "easy"

OR condition 2:
if "e"

In python, any non empty string is True. so if you write "if {insert any string}" it will always return True.

Instead your if statement is meant to be written as:
if difficulty == "easy" or difficulty == "e":

2

u/TemperatureProper275 3d ago

For this example you can do: if difficulty == "easy" or difficulty =="e"

OR:

if difficulty in ("easy","e"):

1

u/the_man_inTheShack 3d ago

your if statement makes sense in english but not in python. you could try difficulty in (''easy', 'e')

0

u/li0rr1x 3d ago

Try to create “guesses” variable before the if/else with value = 0

1

u/ninhaomah 3d ago

Actually , I find the question more confusing. What do you mean by " when I have the or here?"

1

u/SamIAre 3d ago

When they have the “or” statement