def get_yes_or_no(message):
valid_input = False
while not valid_input:
answer = input(message)
answer = answer.upper() # convert to upper case
if answer == 'Y' or answer == 'N':
valid_input = True
else:
print('Please enter Y for yes or N for no.')
return answer
甚至不需要使用valid_input
变量;你可以有一个你return
出来的无限循环:
def get_yes_or_no(message):
while True:
answer = input(message).upper() # convert to upper case
if answer == 'Y' or answer == 'N':
return answer
print('Please enter Y for yes or N for no.')