[Tutor] Python Programming exercise

Sander Sweers sander.sweers at gmail.com
Wed Jul 1 16:07:15 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

Some genral comments.

# You do not need to create the guess variable wirh value "0" as it
gets created in the while loop.
# raw_input() already gives a string so no need to use str() on guess

You will need to break out of the while loop if the password matches.
So add a break under print "Password Confirmed" and it will stop when
you want it to .

Below is a modified (untested) version.

Greets
Sander
----
password = "qwerty"
count = 0
while count != 3:
	guess = raw_input("Enter your password: ")
	if guess != password
		print "Access Denied"
	count = count + 1	
	else:
		print "Password Confirmed"
                break
----


More information about the Tutor mailing list