Reputation: 185
Pretty new to Python, trying to create a simple login system here (has to be done this way). I've already defined a user() function which asks for the username and checks its validity. This function starts by calling the user function. Here is the main part:
user()
if user in userlist:
while True:
pass = raw_input("Enter password, or X to retry: ")
if pass == 'X':
break
if userlist[user] == pass:
break
else:
print "Invalid password."
I want the function to loop back to asking for username input if X is entered. The rest of it works fine but as it stands, entering X just ends the function and doesn't loop it back to the start. Is this just not possible or can I rewrite it to work? I assume I'd need to include user() into the loop but I've encountered various errors while trying.
Upvotes: 0
Views: 390
Reputation: 1
You'll have to do:
if password == 'X':
continue #restarts loop at while
Upvotes: 0
Reputation: 184211
You intentionally say to exit the loop if the user enters X
by using the break
statement. That's why the loop is exiting. Instead of break
use continue
.
if password == 'X':
continue
This will start the loop over again at the top.
As another user notes, you can't use pass
as a variable name. The code you posted clearly isn't the code you're actually running. Anyway, I've assumed that you really used the name password
.
Upvotes: 5
Reputation: 620
Don't use pass as a variable name because it clobbers the builtin pass
. What you want is
if passwd == 'X':
continue #restart at top of loop
Upvotes: 0