Thread: New to python

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

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    New to python


    Please can some one help me identify the error in this code since am new to python. after running it the command prompt just flashes without standing on..


    # Script to protect members of exclusive site
    # MEMBERS ONLY
    # By Patrick Andoh - 18\06\2013

    print "\tWelcome to the exclusive site:"
    print "\t\tPlease you will need to register to be one:"

    security = 0
    username = ""
    while not username:
    username = raw_input("Username:")

    password = ""
    while not password:
    password = raw_input("Password:")

    if username == "Patrick" and password == "Jet":
    print "Hi, Pat."
    security = 5
    elif username == "james" and password == "secret":
    print "Sup, James?"
    security = 3
    elif username == "Frank" and password == "civilization":
    print "Welcome, Frank"
    security 3
    elif username == "Tiger" and password == "Attack":
    print "Hay, how you doing?"
    security = 3
    elif username == "guest" and password == "guest":
    print "You are a Limited User, Please sign up"
    security = 1
    else:
    print "Login failed, Forgotten your password?"

    raw_input("\n\nPress the enter key to exit.")
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    2
    It worked on my comp after fixing the indentation and putting a = in one of your security lines.
    Whenever you are posting code to this forum, wrap your code in [ CODE ] [ / CODE ] (without the spaces) so we can see your indentation.

    If I were you, I would also add
    #!/usr/bin/env python2
    and
    # coding=utf-8
    to the top of your file, so it's abit more compatible in linux, and so your have utf support incase you want to use any weird characters.

    I would also rather use while password == "":
    instead of while not password:

    just makes it abit easier to read, I think.

    btw, if you want to debug something, a nice way to do it is to add the line: import pdb;pdb.set_trace() where you want to stop your program, so you can run through your lines step by step and write in commands as well to test variables or change variables and such.
    Code:
    #!/usr/bin/env python2
    # coding=utf-8
    
    # Script to protect members of exclusive site
    # MEMBERS ONLY
    # By Patrick Andoh - 18\06\2013
    
    print "\tWelcome to the exclusive site:"
    print "\t\tPlease you will need to register to be one:"
    
    security = 0
    username = ""
    while username == "":
        username = raw_input("Username:")
    
    password = ""
    while password == "":
        password = raw_input("Password:")
    
    if username == "Patrick" and password == "Jet":
        print "Hi, Pat."
        security = 5
    elif username == "james" and password == "secret":
        print "Sup, James?"
        security = 3
    elif username == "Frank" and password == "civilization":
        print "Welcome, Frank"
        security = 3
    elif username == "Tiger" and password == "Attack":
        print "Hay, how you doing?"
        security = 3
    elif username == "guest" and password == "guest":
        print "You are a Limited User, Please sign up"
        security = 1
    else:
        print "Login failed, Forgotten your password?"
    
    raw_input("\n\nPress the enter key to exit.")
    Last edited by leonnaley2; July 21st, 2013 at 07:24 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    2
    and, your next step would be to remove the code that repeats itself, maybe learn how to use dictionaries and lists for that.
    I would go for something like this:
    Code:
    #!/usr/bin/env python2
    # coding=utf-8
    
    # Script to protect members of exclusive site
    # MEMBERS ONLY
    # By Patrick Andoh - 18\06\2013
    
    print "\tWelcome to the exclusive site:"
    print "\t\tPlease you will need to register to be one:"
    
    username = ""
    password = ""
    security = 0
    
    dd = {"Patrick": {"Password": "Jet", "Security": 5},
          "Guest": {"Password": "Guest", "Security": 1}}
    
    while username == "":
        username = raw_input("Username:")
    while password == "":
        password = raw_input("Password:")
    
    
    if username in dd:
        if password == dd[username]["Password"]:
            print "Hey, ", username
            security = dd[username]["Security"]
            print "Your security clearence is level ", security
        else:
            print "Login failed, Forgotten your password?"
    else:
        print "Login failed, Forgotten your password?"
    
    if security == 1:
        print "You are a Limited User, Please sign up"
    
    raw_input("\n\nPress the enter key to exit.")
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    sudjestion


    Hey, was browsing the forum the other day, found your script interesting. I'm learning Python as well so I decided to play around with your script. Hope you don't mind. Here are the changes I made to it. I put it into Python3 (ex... the print function/input functions have changed) I also added the userExist variable and made the while loop conditional on it being "true" before it contines to prompt the user for a password. Seems to run a little smoother. I'm having a problem with it though, it will only take the first username listed in the "acceptable usernames"... Any sudjestions?

    Code:
    #!/usr/bin/env python2
    # coding=utf-8
    
    # Script to protect members of exclusive site
    # MEMBERS ONLY
    # By Patrick Andoh - 18\06\2013
    
    print("Welcome to the exclusive site:")
    print("Please you will need to register to be one:")
    
    security = 0
    username = ""
    userExists = 0
    while userExists == 0:
        username = input("Username:")
        
        
        if username == ("Patrick" or "james" or "Frank" or "Tiger" or "guest"):
            userExists = 1
        else:
            print("Unrecognized Username...")
        
    password = ""
    while password == "":
        password = input("Password:")
    
    if username == "Patrick" and password == "Jet":
        print("Hi, Pat.")
        security = 5
    elif username == "james" and password == "secret":
        print("Sup, James?")
        security = 3
    elif username == "Frank" and password == "civilization":
        print("Welcome, Frank")
        security = 3
    elif username == "Tiger" and password == "Attack":
        print("Hay, how you doing?")
        security = 3
    elif username == "guest" and password == "guest":
        print("You are a Limited User, Please sign up")
        security = 1
    else:
        print("Login failed, Forgotten your password?")
    
    input("\n\nPress the enter key to exit.")
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    if username == ("Patrick" or "james" or "Frank" or "Tiger" or "guest")

    Logically, 'Patrick' is True. Given the useful way in which python evaluates and/or, the nice looking expression works not as you expect.
    Code:
    >>> "Patrick" or "james" or "Frank" or "Tiger" or "guest"
    'Patrick'
    You can write
    Code:
    if (username=='Patrick')or(username=='james')or...:
    but this would be silly when you can instead write
    Code:
    if username in set('Patrick james Frank Tiger'.split()):
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo