Basic question from pure beginner

Peter Otten __peter__ at web.de
Wed Jul 1 03:01:44 EDT 2009


sato.photo at gmail.com wrote:

> I have been going through some Python Programming exercises while
> following the MIT OpenCourseWare Intro to CS syllabus and am having
> some trouble with the first "If" exercise listed on this page:
> 
> 
http://en.wikibooks.org/wiki/Python_Programming/Conditional_Statements#If_Exercises
> 
> 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.
> 
> My code is here:
> 
> http://python.pastebin.com/m6036b52e
> 
> -daniel

Some hints: 

(1) Have the while loop check for both the correct password and the number 
of tries. Pseudocode:

while <more tries allowed> and <password not entered correctly>:
   ...

(2) After the loop has terminated you can check whether the password was 
entered correctly and print the appropriate message.

Aside: you can't mix if...else with other statements

correct:

if cond:
   print "sunny"
else:
   print "cloudy"
print "weather"

wrong: 

if cond:
   print "norwegian"
print "blue" 
else: # syntax error, else must immediately follow the indented if-block:
   print "moon"

you could fix it like so:

if cond:
   print "norwegian"
print "blue"
if not cond:
    print "moon"

Peter




More information about the Python-list mailing list