[Tutor] Cell Bio Newbie Here

Brian van den Broek bvande at po-box.mcgill.ca
Mon Apr 11 22:45:55 CEST 2005


Alberto Troiano said unto the world upon 2005-04-11 16:09:
> Hey Gary
> 
> password="foobar"
> 
> #######
> 
> the variable password has to be here because you are referiencing before the 
> assignment inside the while sentence. You can also set it to password="" and 
> still will work because you have to tell (in this example) that password is a 
> reserved word(variable)

Hi Alberto, Gary, and all,

Alberto, if I may, I think there are some problems in what you posted. 
(But, if you want to see some *really* troubled postings, just find 
some of my earliest attempts to answer on the tutor list!)

I think it is important to keep the distinction between variables and
reserved words clear.

>>> if = "Won't work as 'if' is a reserved word"
Traceback (  File "<interactive input>", line 1
     if = "Won't work as 'if' is a reserved word"
        ^
SyntaxError: invalid syntax
>>>

"Reserved words" or "keywords" are the fixed words of the Python
language itself. See Section 2.3.1 Keywords of the Language Reference.

> ########
> 
> count=3
> current_count=0
> 
> #######
> 
> Here you have two options:
> 
> Option 1:
> 
> 
> while password !="unicorn":
>     if current_count<count:
>         password=raw_input("Password:")
>         current_count=current_count+1
>     else:  
>         current_count=2  
>         print "That must have been complicated" 
> 
> print "Welcome in"
> 
> Add this line inside the "else" clause: current_count=2 ####This will make you 
> have more chances and if you fail it will complain.

That isn't going to solve the infinite looping problem in the case
that password never is equal to 'unicorn'

> The problem is that current_count doesn't decrement in the loop, so let's say 
> you fail 3 times the current_count will keep looping because its value is 3 and 
> it won't change in your code. Also I think that if you're making an application 
> to restrict the error to 3 times you may want to finish the app to start over so 
> in that case you may want to try option 2. 
> 
> #######                
> 
> Option 2:
> 
> while password !="unicorn" and current_count <= count:
>     if current_count<count:
>         password=raw_input("Password:")
>         current_count=current_count+1
>     else: 
>         current_count=current_count+1
>         print "That must have been complicated" 
> if password="unicorn":  
>         print "Try again Later"
>     else: 
>         print "Welcome in"         
> 
> Here you will lock your prog when the user fails 3 times and will print your 
> line once and then will jump to Try Again later and it will finish

I'm pretty sure there are some crossed wires here. :-) Maybe you
intended the last two print statements to be swapped? But, either way, 
since Option 2 will never exit the while loop unless password does
equal 'unicorn', the final else clause will never be reached. Or, so
it seems to me.

Last, I don't see why the first else block increments current_count.

Best,

Brian vdB




More information about the Tutor mailing list