1

How could I add something (probably a loop), to this code to make it so that if the wrong thing is entered it makes the user start again?

Usernames = ['Bob', 'Dave']
Passwords = ['Password1']

UsernameCheck = str(input("Player 1 - Enter your username: "))
UsernameCheck2 = str(input("Player 2 - Enter your username: "))
PasswordCheck = str(input("Player 1 - Enter your password: "))
PasswordCheck2 = str(input("Player 2 - Enter your password: "))

if UsernameCheck and UsernameCheck2 in Usernames and PasswordCheck and PasswordCheck2 in Passwords: 
    print("Your credentials are valid")
else:
    print("Your credentials are invalid, try again")
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Nobody
  • 31
  • 2
  • 2
    You should *never* save passwords cleartext. Use some hash instead. – ex4 Mar 06 '21 at 16:09
  • Use a while loop and when the RIGHT code is entered, break. Closest thing you'll get to a do-while loop in Python –  Mar 06 '21 at 16:11
  • 1
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Tomerikoo Mar 06 '21 at 16:34

1 Answers1

1

This will work for you:

Usernames = ['Bob', 'Dave']
Passwords = ['Password1']

while True:
    UsernameCheck = str(input("Player 1 - Enter your username: "))
    UsernameCheck2 = str(input("Player 2 - Enter your username: "))
    PasswordCheck = str(input("Player 1 - Enter your password: "))
    PasswordCheck2 = str(input("Player 2 - Enter your password: "))
    if((UsernameCheck in Usernames)and(UsernameCheck2 in Usernames)and(PasswordCheck in Passwords)and(PasswordCheck2 in Passwords)):
        print("Your credentials are valid")
        break
    else:
        print("Your credentials are invalid, try again")
Cute Panda
  • 1,468
  • 1
  • 6
  • 11