teolicht
teolicht

Reputation: 255

If statements executed even with false condition

I am having this annoying problem with if/elif statements. I'm a newbie, sorry for dumb question. I tried finding a fix but no one had it for Python. So, I want the program to execute the code in the if clause if both conditions are True. As far as I know the code in the clause will only execute if both the conditions are True, am I right? That doesn't seem to happen in my code, though.

result = userNumber + randomNumber
if not result % 2 == 0 and userChoice == 'e' or 'even':
    print ('That number is odd, so you lost :(')
if result % 2 == 0 and userChoice == 'e' or 'even':
    print ('That number is even, so you won :)')
if not result % 2 == 0 and userChoice == 'o' or 'odd':
    print ('That number is odd, so you won :)')
if result % 2 == 0 and userChoice == 'o' or 'odd':
    print ('That number is even, so you lost :(')

So, the userNumber and the randomNumber variables were set before. In this game, what happens is: the user chooses even or odd, and inputs a number from 0 to 5. Then, the computer randomly chooses a number from 0 to 5, using random.randint(). After that, the variable result is set to the sum of userNumber + randomNumber. If the result of that sum is odd and the user chose odd, the user wins, and if the user chose even, the user loses. If the sum is even it's exactly the opposite: if the result of the previous sum result is even and the user chose even, the user wins, and if the user chose odd, the user loses. I hope you understand!

So, the problem with my code right now is that it executes all four IF statements for some reason, so the final output looks like this:

Welcome to the Even or Odd game!

Type letter 'o' if you want odd and the letter 'e' if you want even.

Your choice: o

Now, type in a number from 0 to 5: 2

Your number: 2

Computer's number: 5

Adding these two numbers together, we get 7

That number is odd, so you lost :(

That number is even, so you won :)

That number is odd, so you won :)

That number is even, so you lost :(

The code for that is:

import random
import time

print ('Welcome to the Even or Odd game!')
print ('Type letter \'o\' if you want odd and the letter \'e\' if you want even.')

userChoice = input('Your choice: ').lower()
time.sleep(1)
userNumber = int(input('Now, type in a number from 0 to 5: '))

randomNumber = random.randint(0,5)
time.sleep(2)

print ('Your number: ' + str(int(userNumber)))
time.sleep(2)
print ('Computer\'s number: ' + str(int(randomNumber)))
time.sleep(2)

result = userNumber + randomNumber
print (str(result))
print ('Adding these two numbers together, we get ' + str(result))
if not result % 2 == 0 and userChoice == 'e' or 'even':
    print ('That number is odd, so you lost :(')
if result % 2 == 0 and userChoice == 'e' or 'even':
    print ('That number is even, so you won :)')
if not result % 2 == 0 and userChoice == 'o' or 'odd':
    print ('That number is odd, so you won :)')
if result % 2 == 0 and userChoice == 'o' or 'odd':
    print ('That number is even, so you lost :(')

Any ideas? Sorry for long post, and sorry if duplicate! I just didn't find any answers around the internet :/ Thank you so much!

EDIT: I also tried using elif statements instead of all if, didn't work either.

Upvotes: 0

Views: 3927

Answers (1)

cs95
cs95

Reputation: 403128

>>> if False or 'even':
...     print('This shouldn\'t work')
... 
This shouldn't work

The truth value of a non-empty string is True. Although your other conditions are False, you're still executing those statements because of the way you've written your condition.

As it stands, this is how your condition is evaluated:

if (not result % 2 == 0) and ((userChoice == 'e') or ('even')):
# __________1________________________2_________________3_____

(1) is True. (2) is False. But (3) is True (because of the truthiness of strings). So you execute the first if.

What's more, you only want one of these conditions to execute. Not all of them. You'll need to replace your second if and onwards with elif. So, if one condition is True, all the other conditions are skipped.

You'll need to make a small change:

if not result % 2 == 0 and userChoice in ['e', 'even']:
    ...
elif result % 2 == 0 and userChoice in ['e', 'even']:
    ...
elif not result % 2 == 0 and userChoice in ['o', 'odd']:
    ...
elif result % 2 == 0 and userChoice ['o', 'odd']:
    ...

Upvotes: 2

Related Questions