Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macsrockmysocks

macrumors regular
Original poster
Feb 21, 2006
233
0
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:

Code:
# 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

macrumors newbie
Jan 27, 2006
23
0
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

macrumors regular
Original poster
Feb 21, 2006
233
0
CharlesRobinson said:
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

macrumors 6502
May 12, 2003
468
0
macsrockmysocks said:
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.

Code:
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?)

Code:
attempts = attempts + 1

To check for the maximum number of attempts:

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

To manually exit the while loop:

Code:
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

macrumors regular
Original poster
Feb 21, 2006
233
0
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

macrumors regular
Original poster
Feb 21, 2006
233
0
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

macrumors 6502
May 12, 2003
468
0
macsrockmysocks said:
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

macrumors regular
Original poster
Feb 21, 2006
233
0
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

macrumors 6502
May 12, 2003
468
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.