PDA

View Full Version : Help with Python code?




macsrockmysocks
Jul 14, 2006, 02:24 PM
I decided to learn Python after my other thread about which code I should learn, but I am having some difficulty. I need to edit this code to make it keep track of how many times the User got the password wrong. If it is 3, it needs to display "that must have been complicated." This is probably very simple, but I just don't get it.I tried entering some stufff, but I cant get it. Here is the code I need to edit:

# Waits until a password has been entered. Use control-C to break out with out
# the password

#Note that this must not be the password so that the
# while loop runs at least once.
password = "foobar"

#note that != means not equal
while password != "unicorn":
password = raw_input("Password:")
print "Welcome in"



CharlesRobinson
Jul 14, 2006, 03:25 PM
Think functionality, and imagine the different situations that this code will encounter:
1) The user gets the password
2) The user does not get the password

You're going to want to code for each of these situations. As you can tell already, the case with a correct password is already in your code snippet (eg. "you're in").
So work on the second situation. You'll need a counter (starting at 0) and you'll need a separate if guard to break the while loop if too many failed attempts have occurred. Toy around with it to see if you can't get the code to express the function (a password prompt) that you're looking for.

macsrockmysocks
Jul 14, 2006, 03:39 PM
Think functionality, and imagine the different situations that this code will encounter:
1) The user gets the password
2) The user does not get the password

You're going to want to code for each of these situations. As you can tell already, the case with a correct password is already in your code snippet (eg. "you're in").
So work on the second situation. You'll need a counter (starting at 0) and you'll need a separate if guard to break the while loop if too many failed attempts have occurred. Toy around with it to see if you can't get the code to express the function (a password prompt) that you're looking for.

Will you helpl me more if I can't get it? :D

therevolution
Jul 14, 2006, 04:16 PM
Will you helpl me more if I can't get it? :D

So you want a variable that will store the number of attempts the user has made, right? You'll want to initialize it to zero.

attempts = 0

Now, take a look at the program you have so far. Step through it one line at a time. Ask yourself these questions:

When will you want to check how many attempts the user has made so far?

When will you want to increment the number of attempts?

The following code will increment 'attempts' by 1: (does it make sense why this works?)

attempts = attempts + 1

To check for the maximum number of attempts:

if attempts == 3:
print "too many attempts"


To manually exit the while loop:

break

There, you have some new code... now you get to figure out where it goes. :D You're not completely done, but that should get you most of the way there.

macsrockmysocks
Jul 14, 2006, 04:17 PM
It's almost been 2 hours.. I must be stupid or something if I don't see it.

nvm ^ replied.. I am trying that thanks.

macsrockmysocks
Jul 14, 2006, 04:21 PM
OMG!! I GOT IT!
DUde, thanks so much. I owe u one!
wait, just for the safe of mind, i see break up there, but I don't know where to put it?

therevolution
Jul 14, 2006, 04:38 PM
wait, just for the safe of mind, i see break up there, but I don't know where to put it?

If the user hit the maximum number of attempts, do you want to keep prompting them for the password? If not, you'll want to get out of the while loop using 'break'. If you DO you want to keep asking them for the password, then you don't need 'break'.

I assumed you would want to stop asking them, but do whatever the assignment asks you to do.

macsrockmysocks
Jul 14, 2006, 04:48 PM
now the tutorial is asking me to make a program that aks the user to enter 2 numbers and if the 2 numbers add up to 100 or more, it will print This number is big... I am starting to hate this tutorial...

therevolution
Jul 14, 2006, 06:18 PM
Tutorials are pretty much trivial busy work, but you've gotta start somewhere. Try to learn as much as you can from them, then go off and do your own thing when you feel like you're ready.