[Tutor] Python Programming exercise

Wayne srilyk at gmail.com
Wed Jul 1 15:23:21 CEST 2009

On Tue, Jun 30, 2009 at 9:53 PM, Daniel Sato <sato.photo at gmail.com> wrote:

> <snip>

I have been able to make the module quit after entering a password three
> times, but can't get it to quit right away after the correct one is
> entered.  I know this is really basic, please forgive me.  I have no
> programming experience and have just started going through these tutorials

Let's take a quick look at your code:

   1. password = "qwerty"
   2. guess = "0"
   3. count = 0
   4. while count != 3:
   5.         guess = raw_input("Enter your password: ")
   6.         guess = str(guess)
   7.         if guess != password
   8.                 print "Access Denied"
   9.         count = count + 1
   10.         else:
   11.                 print "Password Confirmed"

On line 4 you enter a while loop. What criteria stops the loop?

As an aside - with while loops it's better to use a < or > (or >= <=)
comparison. Because what happens if something goes wrong inside your loop
and count becomes 4? Your loop takes a LONG time to finish! Actually in
python it probably won't ever finish. Also you should move line 9 either
right after line 4 or right after line 11. When you're incrementing a value
it's best to do it at the beginning or end of a loop. (and certainly never
in between an if and else!)

So with this in mind, what could you do once it prints "Password confirmed"
to make sure the loop ends?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090701/cea8f77f/attachment.htm>

More information about the Tutor mailing list