Loop Question

C

christhecomic

How do I bring users back to beginning of user/password question once they
fail it? thx
 
R

rurpy

How do I bring users back to beginning of user/password question once they
fail it? thx

This is not a very good question. There is no context
so we cannot tell if you are talking about a command line
program that prompts for a username and password or a
GUI program with a text box asking for the user/password
or something else. Nor how exactly you go about doing
the asking.

Some code showing what you are trying to do would help
a lot.

I'll guess you're asking about a simple command line
program. Maybe something like the following is what
you want?

while True:
user = input ("User? ").strip()
pw = input ("Password? ").strip()
if check_user (user, pw): break
print ("Hello %s" % user)

check_user() is a function you'll define somewhere that
checks the user and password and returns True if they are
correct and False if not.

Since you didn't say, I'm also assuming you're using
Python 3; for Python 2 the code will be a little
different.

This is a useful guide to asking questions in a way that
makes it easier for people to answer (and thus help
*you* get better, faster answers):
http://www.catb.org/esr/faqs/smart-questions.html
 
S

Steven D'Aprano

How do I bring users back to beginning of user/password question once
they fail it? thx

Write a loop. If they don't fail (i.e. they get the password correct),
then break out of the loop.
 
C

christhecomic

Here is my code...I'm using 2.7.5

username=raw_input("Please enter your username: ")
password=raw_input("Please enter your password: ")
if username == "john doe" and password == "fopwpo":
print "Login Successful"
else:
print "Please try again"
 
L

Lutz Horn

Hi,

Am 24.06.2013 14:12 schrieb (e-mail address removed):
username=raw_input("Please enter your username: ")
password=raw_input("Please enter your password: ")
if username == "john doe" and password == "fopwpo":
print "Login Successful"
else:
print "Please try again"

while not username or not password or username != "john doe" or
password != "fopwpo":
print "Please try again"
username=raw_input("Please enter your username: ")
password=raw_input("Please enter your password: ")

print "Login Successful"
 
R

rusi

Here is my code...I'm using 2.7.5


username=raw_input("Please enter your username: ")
password=raw_input("Please enter your password: ")
if username == "john doe" and password == "fopwpo":
print "Login Successful"
else:
print "Please try again"


Good!

Now take Steven's suggestion "loop with a break"
and bung your code into Steven's loop.
[Hint: You have to add a break somewhere!]

Or at least try!
 
C

christhecomic

How do I bring users back to beginning of user/password question once they

fail it? thx

Can't seem to get this to cooperate...where does the while statement belong?
 
J

John Gordon

Can't seem to get this to cooperate...where does the while statement belong?

while True:
username = raw_input("Please enter your username: ")
password = raw_input("Please enter your password: ")

if username == "john doe" and password == "fopwpo":
print "Login Successful"
break

else:
print "Please try again"
 
C

Chris “Kwpolska†Warrick

while True:
username = raw_input("Please enter your username: ")
password = raw_input("Please enter your password: ")

if username == "john doe" and password == "fopwpo":
print "Login Successful"
break

else:
print "Please try again"

You didn’t test the code, did you? Because the code you posted is
right. Remember to always test code before submitting. And note that
the getpass module is what you should use for that second thing in
real life, for the security of your users.
 
J

John Gordon

You didn't test the code, did you? Because the code you posted is
right.

It's right, therefore I did not test it? I don't understand.

If the code has a bug, please point it out.
And note that the getpass module is what you should use for that second
thing in real life, for the security of your users.

I'm sure this is just an exercise for the OP to understand loops. Security
would be counter-productive.
 
D

Dave Angel

It's right, therefore I did not test it? I don't understand.

I expect that Chris simply misinterpreted the quoting in your message,
thinking that the OP had both typed the
"Can't seem to get this to cooperate...where does t..."
and the correct code. In fact, the OP typed the former, and you typed
the latter.
 
D

Dave Angel

Hi,

Am 24.06.2013 14:12 schrieb (e-mail address removed):

while not username or not password or username != "john doe" or password
!= "fopwpo":
print "Please try again"
username=raw_input("Please enter your username: ")
password=raw_input("Please enter your password: ")

print "Login Successful"

That requires you to have four raw_input() calls instead of two. And
what purpose does adding the two new clauses to the while test serve?
How is:

while not username or not password or username != "john doe" or password
!= "fopwpo":

different from:

while username != "john doe" or password != "fopwpo":

(other than taking more time and space, and being harder to read) ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,135
Messages
2,570,783
Members
47,340
Latest member
orhankaya

Latest Threads

Top