[Tutor] Cell Bio Newbie Here

Jacob S. keridee at jayco.net
Tue Apr 12 04:48:04 CEST 2005


> Ok, it's a logic error in the while loop.  Starting at the beginning: you
> can't compare the value of "password" until the user inputs the value, 
> which
> is why it's requiring you to put password = "foobar" at the top. 
> Otherwise,
> password has no value, and as far as the interpreter is concerned, it 
> doesn't
> exist yet.

Looks good.

> The rest of it is a logic error because your while loop is executing as 
> long
> as "unicorn" != "foobar" and therefore it's continuously looping.  Each 
> time
> it loops, it asks for the password again, and sets a LOCAL variable called
> password to the new raw_input.

So you're telling us that --- 

x = 5
while x != 5:
    print "Not equal"
    x = x+1
print "Equal"

will result in an infinite loop because stating x = x+1 sets a Local 
variable? I don't think so.
Maybe what you're saying is the condition stated with the while loop is only 
executed once?
That's not the case.


Oh by the way Gary -- summing up the answers.

password = raw_input("What's the password? ")
count = 0
good = True
while password != 'unicorn':
    if count > 2:
        print "That must be hard work."
        good = False
        break
    print "That's not it."
    count = count+1
    password = raw_input("What's the password? ")

if good:
    print "Welcome in. "
else:
    print "I'm gonna kick you out. "


Execution is as follows.

We start off by asking the password.
Count is zero.
We assume that password is good  # good = True
We check with while loop condition.
If good, skip loop and print "Welcome in " because good == True
If bad, check if count greater than three. In this case no.
print "That's not it" because whatever the password is, it's not unicorn, or 
we wouldn't be in the loop.
Increment count
Ask another password.
Check to see if password is unicorn.
If it is, good == True, so "Welcome in. " is printed.
If not, check to see if count > 2 i.e. this is the third time. In this case 
it's not, so print "That's not it. "
Increment count
Ask another password.
Check to see if password is unicorn.
If it is, good still equals True, so print "Welcome in."
If not, which it isn't yet, check to see if count > 2.
In this case, it is, so print "This must be hard work."
Set good = False
break out of loop
good != True
so print "I'm gonna kick you out."

HTH,
Jacob 



More information about the Tutor mailing list