[Tutor] password loop

Wayne Werner waynejwerner at gmail.com
Tue Sep 27 13:21:41 CEST 2011


On Fri, Sep 23, 2011 at 5:56 PM, ADRIAN KELLY <kellyadrian at hotmail.com>wrote:

>  Can anyone help me with the programme below; i hope you can see what i am
> trying to do, if i enter the wrong password the loop goes on forever and if
> i enter the right one nothing is printed...
> i am a newbie............all comments welcome
>

I didn't notice anyone else mention it, but this is where the interactive
interpreter really shines, because it allows you to see immediately what the
result is:

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello"
hello
>>> print "I am your computer"
I am your computer
>>> password = 'route9999'
>>> enter_password = raw_input('Please enter your password: ')
Please enter your password: bob
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello"
hello
>>> print "I am your computer"
I am your computer
>>> password = 'route9999'
>>> enter_password = raw_input('Please enter your password: ')
Please enter your password: bob
>>>

>From your code and your variable names, it looks like you're confused as to
what the statement 'enter_password = raw_input('Enter your password')' does.
Because you use the assignment operator (the = sign), the expression on the
right (raw_input('Enter your password')) is evaluated, and then the result
of that operation is assigned to the enter_password variable.

If you use the interactive interpreter you will see that happen immediately,
and there's no confusion about exactly what is happening. Whenever I am
coding (and most Pythonistas, I suspect) I have two windows open - one
containing the code that I'm working on, and another containing an
interactive interpreter (I use IPython which is much more powerful than your
standard interpreter). This allows me to try out snippets (short pieces) of
code, to see what will happen there. It's much quicker to play around in the
interpreter and then copy my code into my actual program, than write my code
and then test to see if it does what I thought it would do.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110927/a0d585ac/attachment.html>


More information about the Tutor mailing list