1
import random
die1=''
die2=''
throw=''
player=''

play=(input("Would you like to play?"))

if play == "Yes" or "yes":
    die1 = random.randint(1,6)
    die2 = random.randint(1, 6)
    throw = (die1+die2)
    print(throw)

else:
    print("Thanks anyways!")

I've been staring at my code for about an hour and I cannot figure out what I have done wrong. Line 9 always comes out as 'True' regardless of the conditional actually being True or not. Could someone please spot what I have done wrong?

3 Answers3

3

Your condition is evaluated in the following way.

(play == "Yes") or bool("yes")

You want it to be

play == "Yes" or play == "yes"

Otherwise if play != "Yes", the truthiness of "yes" is evaluated and non-empty strings are truthy.

A more general case that covers all combinations of lower and uppercases would be to use this.

play.lower() == "yes"
Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
0

"yes" is cast as True. What you need to do is do if play == "Yes" or play == "yes"

Miles
  • 22
  • 4
0

This is how the python interprets play == 'Yes' or 'yes' so when you actually run it in the terminal you see the output as yes and not True or False as when you do or it checks first condition which might be false in your case but then it gives yes which is non empty string which is treated as True value in python.

So to overcome this problem adding on top of @Oiver's answer

you can change the line to

  1. if play == 'Yes' or play == 'yes':
  2. if play in ['Yes', 'yes']:

this should fix the problem.

warl0ck
  • 3,356
  • 4
  • 27
  • 57