Why is an already defined function in Python not executing?
Question:
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
Answers:
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()
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
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()