0

I am doing a python project for my university and in the project I have a login information with user and password, but I want that if the user is wrong, the code repeats again and if the user is correct, the code pass and same case with password if password is wrong code starts from zero and if password is correct code continues with rest but i dont know how can i do this so please help and Thank you very much for the help.

User=input("Type the User: ")


if User=="Admin":
    print("User correct")
    Password=input("Type the password: ")
else:
    print("User incorrect")

#In here if the user are incorrect, I need the firts starover

if Password=="admin":
    print("Nice, continue")
else:
    print("Wrong password")

#In here if the password are incorrect, I need the other starover.
  • who closed this question? isn't sof for beginners? – Hari Kishore Feb 24 '22 at 05:45
  • 1. Welcome to Sof. Please do read about python loops thoroughly, know difference between usecases for while loop and for loop. – Hari Kishore Feb 24 '22 at 05:49
  • 2. `while` loop - is used when you want to terminate your looping based on any condition. `for` loop - is used when you want to loop a certain no. of times. Now, for your use case, you want to keep looping and want to terminate when user enters correct credential (a condition, right?!). – Hari Kishore Feb 24 '22 at 05:50
  • 3. so, something like this could be one alternative: ```python3 user=input("Type the User: ") while (user != "Admin"): #when user is not "Admin", keep looping user = input("Type the User: ") if user != "Admin": print("User incorrect") continue password=input("Type the password: ") if Password=="admin": print("Nice, continue") else: print("Wrong password") ``` – Hari Kishore Feb 24 '22 at 05:50
  • 4. Note: Did you notice I used small caps variable identifiers? `user` instead of `User` and `password` instead of `Password`. that's because we follow the convention of representing a `class` in python with first letter captial (or CamelCase). It definitely is just convention, and wont throw error if you use it either way. Side Note: This is of course, not the only way. If other people write the answer, do go through them, try dry-run, learn the login. – Hari Kishore Feb 24 '22 at 05:51
  • TO one who closed this querstion: I was writing an answer on this question, but before I couldn't submit, you closed. nvm. – Hari Kishore Feb 24 '22 at 05:52

1 Answers1

0

You can use while to do this problem:
Loop until the username/password is correct.
Here is my solution:

User=""
Password=""

while(User != "Admin"):
    User = input("Type the User: ")
    if User=="Admin":
        print("User correct")
    else:
        print("User incorrect")

#Do same thing with password

while(Password != "admin"):
    Password=input("type password")

    if Password=="admin":
       print("Nice, continue")
    else:
       print("Wrong password")
Bao Dinh
  • 64
  • 6
  • The canonical way would be a `while True` loop where you `break` from it if a given condition is satisfied. – Matthias Feb 24 '22 at 06:42