[Tutor] While Loop
Dave Angel
d at davea.name
Wed Jun 13 07:59:02 CEST 2012
On 06/13/2012 01:29 AM, Andrew Brunn wrote:
> Let me preface by saying that I am currently taking my first programming
> class; so I have been a "programmer" for just over a week now.
>
> Here is my question;
>
> I
> am trying to create a while loop that will allow me ask for a username
> and password. If a wrong username and password is entered, I would like
> the program to prompt the user to re-enter the username and password
> until they get it correct. That means even if they get it wrong a
> thousand times, I want to program to keep asking them.
>
> This is my code;
>
> [code]
> username = ""
>
> while not username:
> username=raw_input("Username: ")
>
> password = ""
>
> while not password:
>
> password=raw_input("Password: ")
> if username == "john doe" and password == "fopwpo":
> print "Login successful"
> else:
> print "Error. Please re-enter your username and password."
> [code]
>
>
>
> Andy Brunn
>
So put the whole thing in a while loop. For that matter, also put it in
a function. Anything over about 3 lines should be written as one or
more functions.
The simplest change would be to make the while loop look like while
True:, and put a break right after the print "Login successful" line,
def getUserPass():
while True
(existing code here)
if username == "xxxx" and password == "yyyy":
print "login successful"
break
else:
print "Error: Please ...."
--
DaveA
More information about the Tutor
mailing list