Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I end my while loop
#1
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 somewhere
I don't know how to end the loop
Reply
#2
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")
TechNitium likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jan-22-2021, 02:08 PM)buran Wrote: 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")

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 somewhere
Feel free to try it out and thanks
Reply
#4
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
TechNitium likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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.
Reply
#6
(Jan-22-2021, 04:49 PM)BashBedlam Wrote: 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.

Yep thanks, noticed that and changed it.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020