Help with Python code?

Discussion in 'Mac Programming' started by macsrockmysocks, Jul 14, 2006.

  1. macsrockmysocks macrumors regular

    Feb 21, 2006
    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"
  2. CharlesRobinson macrumors newbie


    Jan 27, 2006
    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.
  3. macsrockmysocks thread starter macrumors regular

    Feb 21, 2006
    Will you helpl me more if I can't get it? :D
  4. therevolution macrumors 6502

    May 12, 2003
    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:

    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.
  5. macsrockmysocks thread starter macrumors regular

    Feb 21, 2006
    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.
  6. macsrockmysocks thread starter macrumors regular

    Feb 21, 2006
    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?
  7. therevolution macrumors 6502

    May 12, 2003
    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.
  8. macsrockmysocks thread starter macrumors regular

    Feb 21, 2006
    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...
  9. therevolution macrumors 6502

    May 12, 2003
    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.

Share This Page