Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

While loop won't end

I have a while loop that calls on three functions. The three functions work as intended and at the end it asks for a user input. If the user input is ‘n’ the loop is supposed to end, but it continues regardless of what I put.

Here’s my code:

yn = ""
while (yn.lower != "n"):
    usr_choice = userChoice()
    com_choice = compChoice()
    winner(usr_choice, com_choice)
    yn = input("Would you like to continue? y/n: ")

I’ve tried formatting the while loop in various ways and ensuring no variable names matched regardless of their location but I still can’t end the loop by typing n or N. If possible I’d like to have the while loop end with either n or no, but first I need to get it working.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The issue is that you’re not actually envoking the yn.lower method, you’re just comparing the function object to the string "n"

yn = ""
while (yn.lower() != "n"): # <<---- See yn.lower VS yn.lower()
    usr_choice = userChoice()
    com_choice = compChoice()
    winner(usr_choice, com_choice)
    yn = input("Would you like to continue? y/n: ")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading