0

I am a Python NOOB and I have a Python function game() that executes perfectly, when the game ends I ask the player to play again. The input and if statement works (it prints my corny comment) but the next invocation of game() doesn't execute and the program ends? Why?

# Lots of cool game() code above here ^^^
game() #first invocation of the game() function, works great...
print ("Play Again? Y=YES, Any other key to quit")
if input() == "y" or "Y":
    print("oh yeah, its on!!!!")
    game()
else:
    exit
discoStew
  • 225
  • 2
  • 8
  • Because that's not how the "or" operator works. Try `if input() in "yY":`. – Tim Roberts Dec 05 '22 at 04:59
  • Same result, the `print("oh yeah, its on!!!!")` executes using both `if input() in "yY":` OR (haha) `if input() == "y" or "Y":`, but the function still doesn't execute. – discoStew Dec 05 '22 at 05:02

1 Answers1

1

The issue likely with the following statement

if input() == "y" or "Y": 
    ...

Which is equivalent to

if (input() == "y") or ("Y"): 
    ...

You could fix that by preprocessing the string

while True:
    user_response = input('Play Again? Y=YES, Any other key to quit').lower() #convert to lower case
    if user_response == 'y': 
        game()
    else: 
        break

Compact

while input("Play Again? Y=YES, Any other key to quit").lower() == 'y':
    game()