Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop not closing
#1
I'm using a code with a while loop:

def main():
    print("1) Create a new user ID") # prints this menu
    print("2) Change a password")
    print("3) Display all user ID's")
    print("4) Quit")
    print()
    choice = int(input("Please enter your selection:")) # asks user for their selection
    try_again = True
    while try_again == True:
        if choice == 1:
            tmp = get_data() # gets tmp list from get_data
            New_ID = create_ID(tmp) # gets New_ID from create_ID
            password = create_password() # gets password 
            new_record = New_ID + "," + password # creates entry string
            tmp.append(new_record) # adds string to the end of tmp
            file = open("ID+Passwords.csv","w") # opens csv file in write mode
            for row in tmp:
                file.write(new_record) # writes every row of the list to the csv file
            file.close # closes the file and saves the changes 
            choice = int(input("Please enter your selection:")) # asks user for their selection
        elif choice == 2:
            tmp = get_data()
            New_ID = create_ID(tmp)
            change_password(New_ID,tmp)
            choice = int(input("Please enter your selection:")) # asks user for their selection
        elif choice == 3:
            tmp = get_data()
            display(tmp)
            choice = int(input("Please enter your selection:")) # asks user for their selection
        elif choice == 4:
            try_again == False
        else:
            choice = int(input("Incorrect choice please select a choice from the list:"))
However when I select option 4 the loop isn't closing properly, this is what my terminal looks like:

Please enter the new ID1
Please enter the new password:numbers0123
That is a strong password
Please enter your selection:4

Rather than going to the normal non-running terminal it just has a blank line underneath. Can anyone help with this?
Reply
#2
You need to use = instead of == on line 31.
Reply
#3
try_again = False
one = not two on line 31
A simple print statement immediately after line 9 will help in cases like this

    while try_again == True:
        print(f"choice: {choice}, try_again: {try_again}"}
Reply
#4
Thanks for the help :)
Reply
#5
Look how often you ask for input. Why make a variable just to break out of the loop?
def main():
    print("1) Create a new user ID")
    print("2) Change a password")
    print("3) Display all user ID's")
    print("4) Quit")
    print()
    while True:
        choice = input("Please enter your selection:")  # Why make it an integer???
        if choice == '1':
            data = getdata()
            newid = createid(data)
            password = createpassword()
            newrecord = newid + "," + password
            data.append(newrecord)
            with open("ID+Passwords.csv","w") as file # Closes file when done
                for row in data:
                    file.write(newrecord)
        elif choice == '2':
            data = getdata()
            changepassword(createid(data), data)
        elif choice == '3':
            display(getdata())
        elif choice == '4':
            break
        else:
            print("Invalid choice.")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Try-except in while loop: error with closing program Lupin_III 7 2,878 Jul-03-2020, 10:57 AM
Last Post: Lupin_III

Forum Jump:

User Panel Messages

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