Search code examples
pythonloopsfilewhile-loopexit

While != "specific string" loop mishap


I'm trying to make a task a little more efficient. I'm going to continue to research/edit while moving forward.

What the code currently does is ask for your first/last name it then outputs a username & password. Up to this point it works fine if there is a more efficient way to rewrite this also appreciated.

The only thing I still need to fix is looping entries(First_name and Last_name) so I can have multiple users added with a \n\n for easy reading/separation of each user. preferably with a exit command such such as "done"

I'm also going to be looking into how to create a new file that this information is stored in but this is the section I'm going to try to figure out.

First_name = input("Enter your First name: ")
Last_name= input("Enter your last name: ")


Username = str(First_name) + str(Last_name)
Password = Username + str('1!')


print("Username: ", Username.upper())
print("Password: ", Password)

In terms of trying to solve the issue.

I did try to create something in the ball park of..

First_name = input("Enter your First name: ")
Last_name= input("Enter your last name: ")

while First_name != "done":
    Last_name= input("Enter your last name: ")
    First_name = input("Enter your First name: ")
    Last_name= input("Enter your last name: ")
    Username = str(First_name) + str(Last_name)
    Password = Username + str('1!')
    print("Username: ", Username.upper())
    print("Password: ", Password)

this just repeated the first two lines without printing the Username/password. It also appeared to acknowledge when I typed done but slightly off from desired. Below I included how the code ran

Enter your First name: first
Enter your last name: last
Enter your last name: last
Enter your First name: done
Enter your last name: last
Username:  DONELAST
Password:  donelast1!

I get why its first, last, last, first last. I dont get how to make the while loop using "while Fist_name !='done': " without putting lines 1 and 2 before the loop. I tried removing the inputs at line 5 and 6 but then it just looped the username and password.


Solution

  • You can use a while loop and break it when the first name is done.

    while True:
        first_name = input("Enter your first name (done to exit): ")
        if first_name.lower() == "done":
            print("Done!")
            break
        last_name = input("Enter your last name: ")
        Username = str(first_name) + str(last_name)
        Password = Username + str('1!')
        print("Username: ", Username.upper())
        print("Password: ", Password)
        print("\n")
    

    Output:

    Enter your first name (done to exit): John
    Enter your last name: Doe
    Username:  JOHNDOE
    Password:  JohnDoe1!
    
    
    Enter your first name (done to exit): Alice
    Enter your last name: Court
    Username:  ALICECOURT
    Password:  AliceCourt1!
    
    
    Enter your first name (done to exit): done
    Done!