Thread: Python-Password

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Python-Password


    I have this code I'm working on. I'm trying to go back to the start of the program and keep on going back to the start of the program until the user enters the right password. Does anyone know what the python code would be in order for me to do this? the program does not have to quit, I want the program to keep on asking the user "Enter your password" until the user puts in "letmein". then I would continue to the rest of the program.

    see code below, this is what I have so far.

    #Password
    password=raw_input("Enter your password:")

    if password == "letmein":
    print "hey your in"
    else:
    print"Denied"
    password=raw_input("Enter your password:")
    print"try again"
    password=raw_input("Enter your password:")
    print"Sorry, you got to go now"
    raw_input ("Press enter to exit")
    quit()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Code:
    import sys
    for attempts in range(2):
        if 'some secret' == raw_input('password: '):
            break
        print('try again')
    else:
        print('Pilot license revoked!')
        sys.exit(1)
    
    print('<horse enter saloon>  Bartender: "Why the long face?"')
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    606
    Rep Power
    65
    Contributing User b49P23TIvg is correct, you really should limit the number of attempts to something reasonable.

    You can rewrite your code like this:
    Code:
    #Password
    
    limit_attempts = 5
    attempts = 1
    while True:
        password = raw_input("Enter your password: ")
        if password == "letmein":
            print "Hey your in!"
            break
        else:
            print "Denied, try again!"
        if attempts >= limit_attempts:
            print"Sorry, you got to go now!"
            break        
        attempts += 1
        
    
    raw_input("Press enter to exit")
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo