[Tutor] embedding an if

Karl Pflästerer sigurd at 12move.de
Thu Apr 1 19:55:22 EST 2004


On 29 Mar 2004, peter hodgson <- py at humnet.ucla.edu wrote:

> tutorial assignment; what am i doing wrong? thanks

I think that password programm is one of the most often mentioned
programs here :-)

> #Modify the password guessing program to keep track of how many times
> # the user has entered the password wrong. If it is more than 3 times,
> # print "you're screwed, buddy!", or something like that;

> passwd = "foobar"		#a dummy passwd
> count = 3
> current_count = 0
> while passwd != "unicorn":
>       current_count = current_count + 1
>       passwd = raw_input("Passwd: ")	
>          if current_count < count:
>              print "no, stupid! try again;"
>          else print "three times, you're out!"
> print "welcome in"

(you include tabs in your e-mails; that may result in wrong indentation
in some e-mail clients or programs if someone tries your code)

Did you try that program?  It won't run.

If you want to wite an else clause it's written like that:

if foo == bar:
    baz
else:
    print 'Python'

Even if you program ran it wouldn't run in the way you probably
expected; after printing the message you need a way to break the loop:
the statement is called `break' (surprise).

But then your program will process the code after the loop so a user
would always see the welcome message.  If you don't want that you need a
way to stop the whole program; you must import the sys module and
`sys.exit()'.


   Karl
-- 
Please do *not* send copies of replies to me.
I read the list




More information about the Tutor mailing list