matt ward matt ward - 1 month ago 6
Python Question

Input needing to be entered twice for my code to work

I'm fairly new to coding and have been assigned a class project, in this project to make a game I was trying to write a function that upon losing/dying the

def playAgain():
function would ask the user if they want to play again.

from sys import exit
def playAgain():
print('Do you want to play again? (yes or no)')
while True:
if input("> ").lower().startswith('yes')== True:
start()
elif input("> ").lower().startswith('no')== True:
print ('Bye for now')
exit(0)
else:
print ("I don't understand what you mean?")


This function 'should' ask the user if they want to play again and depending on if yes or no was entered it would either go to the function
start()
or exit.

The issue is that when the input is entered the first time it is seemingly ignored in the code and must be entered a second time for any thing to happen in the code.

This has confused me a fair amount so any input on how to resolve this issue would be greatly appreciated.

Side note - this issue doesn't appear to happen when yes is entered first meaning this is probably an issue with the
elif
or
else
statements

Answer
from sys import exit

def playAgain():
    print('Do you want to play again? (yes or no)')
    while True:
        choice = input("> ")
        if choice.lower().startswith('yes'):
            start()
        elif choice.lower().startswith('no'):
            print ('Bye for now')
            exit(0)
        else:
            print ("I don't understand what you mean?")  
Comments