0

I'm trying to make the code repeat the line "player name invalid" and ask for the input repetively until the input is "player 1". How do i do that?

correct_n="player 1"
while True:
    Name1 = input ("Enter Your Name: ")
    if Name1 == correct_n:
        cp = 'password'
        while True:
            password= input("enter the password ")
            if password == cp:
                print ("yes you are in")
                break
            print("please try again")
        else:
            print("Player name not valid")
    break
print("player name invalid")

The code just prints "player name invalid" and goes on to do the rest of the code. I don't want the rest of the code to be outputted until the user inputs the correct name and password.

Mali
  • 1
  • 2
  • Do you understand what a `break` statement does, and is for? – Edward Peters Nov 24 '22 at 19:40
  • You don't have to use `break`. Use `continue` `if Name1 != corrent_n:`, `continue` which will keep on looping if condition does not match – GodWin1100 Nov 24 '22 at 19:41
  • You don't need to use `continue` to make it keep looping, that's the default behavior. `continue` will skip the rest of the current loop iteration and restart from the beginning. You'd only use it if there are further statements you don't want to run for the current iteration. – Edward Peters Nov 24 '22 at 19:43
  • Does break not stop the lines before from running if its false? – Mali Nov 24 '22 at 19:50

2 Answers2

0

Because you have two while loops, it is not possible to use break to exit both of them. Instead, you should separate the loops so that the name loop runs until the name is correct, and then the password loop runs until the password matches.

correct_n="player 1"
while True:
    Name1 = input("Enter Your Name: ")
    if Name1 == correct_n:
        break
    else:
        print("Player name not valid")

cp = 'password'
while True:
    password= input("enter the password ")
    if password == cp:
        print ("yes you are in")
        break
    else:
        print("please try again")
Dash
  • 1,191
  • 7
  • 19
-1

Solution:

while input('Enter your name: ') != 'player 1': print('Player name invalid')
walker
  • 444
  • 2
  • 12