I have a main method in Python 2.7.11 that (after its first execution) will ask if the user wants to continue (y/n). The response of 'y' re-executes the while loop I instantiated in main just fine, and erroneous inputs are taken into account and the question is re-asked. However, when the user enters 'n', it does not print 'Goodbye.', it instead exits out of the loop without even reaching the print statement. Here is my code:
def main():
continue = 'y' # Default for the first execution
while continue == 'y':
# A bunch of execution code here for the program...
# After executing rest of code
continue = raw_input('Do you wish to continue? (y/n): ')
while continue != 'y' and continue != 'n':
if will_continue == 'n':
print 'Goodbye.'
else:
continue = raw_input('Invalid input. Do you wish to continue? (y/n): ')
if __name__ == "__main__":
main()
I thought maybe my issue was and in while continue != 'y' and continue != 'n':, so I changed it to while continue != 'y' or continue != 'n':, but this keeps me in an infinite loop of 'Goodbye' being printed if I input 'n', or infinite unresponsiveness if I input 'y'.
Any ideas as to why that print 'Goodbye.' statement won't execute before terminating the main?
Aucun commentaire:
Enregistrer un commentaire