Best way to do this?

W

Wil Schultz

I am reading the "Non-Programmers Tutorial For Python" by Josh Cogliati
http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html

One of the exercises asks for a program to ask for a password three
times. Surprisingly this took me much longer that it probably should
have so I am curious if the following is the easiest way this is done.
Thanks much!

*************************************************
#!/usr/local/bin/python

count = 0
password = "null"

while count < 3:
count = count + 1
password = raw_input("what's the pass: ")
if password == "mypass":
print "Access Granted"
count = 3
else:
if count < 3:
print "Try Again"
else:
print "Too Bad"
*************************************************
 
J

Jeff Epler

def getpass(password, count=3):
for i in range(count):
p = raw_input("Password: ")
if p == password: return 1
return 0

No need to initialize count to 0 and use a while loop, use a "for" loop
instead. No need to initialize password above the loop (and if you had
to, use None instead of "null"), and no need to set count to 3 when the
correct password is entered---just use return or break.

Jeff
 
J

Jeremy Yallop

Wil said:
One of the exercises asks for a program to ask for a password three
times. Surprisingly this took me much longer that it probably should
have so I am curious if the following is the easiest way this is done.
Thanks much!

*************************************************
#!/usr/local/bin/python

count = 0
password = "null"

while count < 3:
count = count + 1
password = raw_input("what's the pass: ")
if password == "mypass":
print "Access Granted"
count = 3
else:
if count < 3:
print "Try Again"
else:
print "Too Bad"
*************************************************

I'd write it something like this:

from getpass import getpass
attempts, password = 3, 'mypass'

for i in range(attempts):
if i != 0:
print 'Try again'
if getpass("What's the pass: ") == password:
print 'Access Granted'
break
else:
print 'Too bad'

Jeremy.
 
W

Wil Schultz

Well, not quite that far yet but apparently there are a few different
way's. Mine of course being the most primitive! ;)

Thank you both!

Wil
my 2¢
"When everything seems to be going well, you have obviously overlooked
something."
 
W

William Park

Wil Schultz said:
One of the exercises asks for a program to ask for a password three
times. Surprisingly this took me much longer that it probably should
have so I am curious if the following is the easiest way this is done.
Thanks much!

*************************************************
#!/usr/local/bin/python

count = 0
password = "null"

while count < 3:
count = count + 1
password = raw_input("what's the pass: ")
if password == "mypass":
print "Access Granted"
count = 3
else:
if count < 3:
print "Try Again"
else:
print "Too Bad"
*************************************************

For crying out loud, here's 3 times for you...
password = raw_input("what's the pass: ")
password = raw_input("what's the pass: ")
password = raw_input("what's the pass: ")
 
W

Wil Schultz

Okay, maybe I should have said the program should ask for the password
up to 3 times, exiting on a) the correct password b) the third incorrect
password.

Wil
my 2¢
"When everything seems to be going well, you have obviously overlooked
something."
 
B

Bruno Desthuilliers

Wil Schultz wrote:
(Wil, please don't top-post... corrected)Beware of specs, programmers can have strange interpretations sometimes !-)

import sys
for i in range(3):
if raw_input("what's the pass: ") == "mypass":
print "Access granted"
sys.exit(0)

print "Access denied"
sys.exit(1)

HTH
Bruno
 
S

sambo

The "obscure" for/else can help:


def get_password (prompt):
for i in (1, 2, 3):
password = raw_input (prompt)
if password == 'mypass':
return True
else:
print "Try Again"
else:
print "Too Bad"
return False



Regards. Mel.
Obscure? at least it does something.

What is the advantage of the following assignment ( a few messages up)

attempts, password = 3, 'mypass'

Reading difficulty - at least 3 (no relation, hehe)

Ciao
 

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,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top