[Tutor] Python Programming exercise

Dave Angel davea at ieee.org
Wed Jul 1 20:42:03 CEST 2009


Daniel Sato  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
>
>   
(for short functions, it's probably better to just include it in your 
message.  That way, they'll be archived together, in case somebody is 
reading this message a year from now.)

You have no context.  Are you going to actually do something with the 
password, or are you done when the confirmation has been printed out?

If the latter, just add a line after the print statement, setting count 
= 99.  You could use a break as well, but then you wouldn't have 
anything to indicate whether the password was ever correctly entered.


Normally, this would be inside a function (def), which might return a 
boolean indicating whether the password succeeded or not.  In that case, 
you might put a return instead.




More information about the Tutor mailing list