[Tutor] How to print something just after 3 attempts?

Mark Lawrence breamoreboy at yahoo.co.uk
Tue Jul 17 18:31:07 CEST 2012


On 17/07/2012 16:28, Paul McNally wrote:
>
>
>
>> Date: Tue, 17 Jul 2012 11:23:07 -0400
>> From: joel.goldstick at gmail.com
>> To: sntshkmr60 at gmail.com
>> CC: tutor at python.org
>> Subject: Re: [Tutor] How to print something just after 3 attempts?
>>
>> On Tue, Jul 17, 2012 at 11:10 AM, Santosh Kumar <sntshkmr60 at gmail.com> wrote:
>>> Hello There,
>>>
>>> This problem isn't so simple as it looks in the title/subject of this email.
>>>
>>> I am doing a tutorial on Python, and there is a script named password.py:
>>>
>>> password = "foobar"
>>>
>>> while password != "unicorn":
>>>      password = raw_input("Password: ")
>>> print "Welcome in"
>>>
>>>
>>> The question says "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 “That must have been complicated.”
>>>
>>> So I did, I did it in two ways.
>>> Case I:
>>> password = "foobar"
>>> attempt = 0
>>> while password != "unicorn":
>>>      password = raw_input("Password: ")
>>>      attempt = attempt + 1
>>>      if attempt == 3:
>>>          print "That must have been complicated.."
>>>
>>> print "Welcome in.."
>>> # This script does the half of work. This prints that statement after
>>> three attempts but lets you enter password more time.
>>>
>>>
>>> Case II:
>>> password = "foobar"
>>> attempt = 0
>>> while password != "unicorn":
>>>      password = raw_input("Password: ")
>>>      attempt = attempt + 1
>>>      if attempt == 3:
>>>          print "That must have been complicated.."
>>>          break
>>> print "Welcome in.."
>> move the print "Welcome under break:
>>            break
>>            print "Welcome..."
>>
>>> # This script performs better than first one, but the problem is it
>>> welcomes you even if you haven't entered the password correct.
>>>
>>> Please tell, what can I do?
>>> _______________________________________________
>>> Tutor maillist  -  Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>>
>> --
>> Joel Goldstick
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
> I was able to get it working like this...
>
> password = "foobar"
> attempt = 0
> while (password != "unicorn") and (attempt <= 3):

Please we're talking Python here not C so strip out those unneeded 
parenthesis.

>      password = input("Password: ")
>      attempt = attempt + 1
>      if attempt == 3:
>          print ("That must have been complicated..")
>          break
>
> if (password == "unicorn"):

ditto.

>      print ("Welcome in..")
>
>
> Not sure if it helps.
>
> - Paul
>   		 	   		
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-- 
Cheers.

Mark Lawrence.





More information about the Tutor mailing list