[Tutor] Python Programming exercise

Siim Märtmaa foobar8 at gmail.com
Wed Jul 1 11:06:24 CEST 2009


2009/7/1 Daniel Sato <sato.photo at gmail.com>:
> 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 Sato
> http://www.danielsato.com
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>

Hello daniel, some comments
password = "qwerty"
guess = "0"  # <- no need to initialise that variable or
             # if you would need, using just "" would be enough #instead of "0"

count = 0
while count != 3:  # <- that works here, but i usually use "< 3"
	guess = raw_input("Enter your password: ")
	
        guess = str(guess)  # no need to typecast here because
raw_input alredy returns a str
                            #when you don't know the type use
type(yourvar) in python

	if guess != password  # missing ":"
		print "Access Denied"
	count = count + 1  # <- has to be indented to the same level with the
above line
                                   # also you can use count += 1 instead
	else:
		print "Password Confirmed"
		# to exit you can use the exit() function


More information about the Tutor mailing list