How do I end my while loop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How do I end my while loop (/thread-32128.html) |
How do I end my while loop - TechNitium - Jan-22-2021 choice = input("") c = ("enter") while c != ("exit"): if (choice) == (2): new_entry() c = ("enter") #if you select option 2 it will call the new_entry code else: choice = input("ERROR\nInvalid input: Please type agian\n") #Store this somewhereI don't know how to end the loop RE: How do I end my while loop - buran - Jan-22-2021 input will return str , so you need to compare choice with '2' or convert the user input to int and then you can compare with 2 .Then to end the loop choice must become 'exit' also, I must comment on excessive use of brackets - no need of brackets in cases like ("enter")
RE: How do I end my while loop - TechNitium - Jan-22-2021 (Jan-22-2021, 02:08 PM)buran Wrote: So your solution did work but when I input an incorrect value but after that, I input the correct value '2' it still says error. #getpass will hide what the person is typing during the password import getpass def new_entry(): #input the username and password and the website Website = input("Enter the website: ") Username = getpass.getuser() Password = getpass.getpass() #Menu to choose if you want to add, see or remove print(" *************************") print(" /$$ /$$ /$$$$$$ /$$$$$$$ \n| $$ | $$|_ $$_/| $$__ $$\n| $$ | $$ | $$ | $$ \ $$\n| $$$$$$$$ | $$ | $$ | $$\n| $$__ $$ | $$ | $$ | $$\n| $$ | $$ | $$ | $$ | $$\n| $$ | $$ /$$$$$$| $$$$$$$/\n|__/ |__/|______/|_______/ ") print("**************************") print ("1. See the list\n2. Add new entry") choice = int(input("")) c = "enter" while c != ("exit"): if (choice) == (2): new_entry() c = ("enter") #if you select option 2 it will call the new_entry code else: choice = input("ERROR:\nInvalid input: Please type agian\n") #Store this somewhereFeel free to try it out and thanks RE: How do I end my while loop - buran - Jan-22-2021 so, you convert the user input on line 16, but do not convert on line 25. In any case, because you don't use the user input for calculations, better compare with str, instead of doing conversion RE: How do I end my while loop - BashBedlam - Jan-22-2021 This is how I would go about it: #getpass will hide what the person is typing during the password import getpass def new_entry(): #input the username and password and the website Website = input("Enter the website: ") Username = getpass.getuser() Password = getpass.getpass() print (Password) while True : print ('1. See the list\n2. Add new entry\n3. Exit now') choice = input ('') if choice == '1' : print ('You chose number one.\n') elif choice == '2' : new_entry() elif choice == '3' : break else: print ("ERROR:\nInvalid input: Please type agian\n")And just FYI, the line Username = getpass.getuser () is not getting input from the user it is grabbing the username of whoever is currently logged in to the computer that this is running on.
RE: How do I end my while loop - TechNitium - Jan-22-2021 (Jan-22-2021, 04:49 PM)BashBedlam Wrote: This is how I would go about it: Yep thanks, noticed that and changed it. |