0

Im trying to create code that prints a game's difficulty level based on what the "diffChoice" value is using IF statements. However, I can't get the "gameDiff" value to correctly represent what the "diffChoice" was. Any help/feedback is appreciated.

diffChoice = 'm'

if diffChoice == 'e' or '1':
    gameDiff = 'EASY'
if diffChoice == 'm' or '2':
    gameDiff = 'MEDIUM'
if diffChoice == 'h' or '3':
    gameDiff = 'HARD'

print(gameDiff)
Drosii
  • 25
  • 4

1 Answers1

0

In all of your Boolean conditions, the second expression after the or is truthy, so x or y will always yield True.

Consider:

if "1":
    print("I'll print!")

if "2":
    print("So will !")

if "":
    print("I won't print; the empty string (an empty sequence of characters) is treated as False.")

if []:
    print("I won't print: the empty list is treated as False.")

if False or 1:
    print("I WILL print: False or 1 is treated as False or True.")

You can read more about this here. In particular:

By default, an object is considered true unless its class defines either a bool() method that returns False or a len() method that returns zero, when called with the object.

What you want is to test a or b, so write:

if diffChoice == 'e' or diffChoice == '1':
    gameDiff = 'EASY'

or, equivalently:

if diffChoice in ('e', '1'):
    gameDiff = 'EASY'

and so on.

crcvd
  • 1,465
  • 1
  • 12
  • 23