How to make script like this?

$python script.py
1. Register
2. Login
3. View User
Choose with number :

I can make a script for Register and Login, but i still confusing make a script for view user.
Spoiler For View User:

Choose with number : 3 (If still not login yet)

"Please Login First!"

Choose witn number : 3 (After Login)

[{'username' : xxxx , 'password' : xxx}]

Here the script:

import hashlib ,os
resource_file = "passwords.txt"

def encode(username,password):
    return "$%s::%s$"%(username,hashlib.sha1(password).hexdigest())
def add_user(username,password):
    if os.path.exists(resource_file):
        with open(resource_file) as f:
            if "$%s::"%username in f.read():
                raise Exception("user already exists")
    with open(resource_file,"w") as f:
         print >> f, encode(username,password)
    return username

def check_login(username,password):
    with open(resource_file) as f:
        if encode(username,password) in f.read():
           return username



def create_username():
     try: 
         username = add_user(raw_input("enter username:"),raw_input("enter password:"))
         print "Added User! %s"%username
     except Exception as e:
         print "Failed to add user %s! ... user already exists??"%username

def login():
     if check_login(raw_input("enter username:"),raw_input("enter password:")):
        print "Login Success!!"
     else:
        print "there was a problem logging in"


def view_user:
       #Still Dont know

menu = """

1. Login
2. Register
3. View User """

print menu 

while True:

    {'2':create_username,'l':login, '3' view_user}.get(raw_input("Chosee with number (1,2,3: ").lower(),login)()

Note: xxxx is just sample for username and password has been created by register...
I'm using Python 2.7
OS windows 7

Edited 8 Months Ago by Arsal_1

You need to store state variables. The best thing to do is to create a class instance

class App(object):
    def __init__(self):
        self.username = None
        self.password = None

    def login(self):
        if self.username is not None:
            print "Already Logged In"
            return
        u = raw_input("enter username:").strip()
        p = raw_input("enter password:").strip()
        if check_login(u, p):
            self.username = u
            self.password = p
            print "Login Success!!"
        else:
            print "there was a problem logging in"

    def view_user(self):
        if self.username is None:
            print "Please Login First!"
        else:
            print (self.username, self.password)

app = App()
class App(object):
    def __init__(self):
        self.username = None
        self.password = None

    def login(self):
        if self.username is not None:
            print "Already Logged In"
            return
        u = raw_input("enter username:").strip()
        p = raw_input("enter password:").strip()
        if check_login(u, p):
            self.username = u
            self.password = p
            print "Login Success!!"
        else:
            print "there was a problem logging in"

    def view_user(self):
        if self.username is None:
            print "Please Login First!"

        else:
            print (self.username, self.password)



app = App()



menu = """

1. Login
2. Register
3. View User """

print menu 
while True:
    {'2':App,'l':login, '3': view_user}.get(raw_input("Chosee with number (1,2,3: ").lower(),login)()

If i put like this, command for login and view user not defined

Use something like

while True:
    {'2':create_username,'l':app.login, '3' app.view_user}.get(raw_input("Chosee with number (1,2,3: ").lower(),app.login)()
import hashlib ,os
resource_file = "passwords.txt"

def encode(username,password):
    return "$%s::%s$"%(username,hashlib.sha1(password).hexdigest())
def add_user(username,password):
    if os.path.exists(resource_file):
        with open(resource_file) as f:
            if "$%s::"%username in f.read():
                raise Exception("user already exists")
    with open(resource_file,"w") as f:
         print >> f, encode(username,password)
    return username

class App(object):
    def __init__(self):
        self.username = None
        self.password = None

    def login(self):
        if self.username is not None:
            print "Already Logged In"
            return
        u = raw_input("enter username:").strip()
        p = raw_input("enter password:").strip()
        if check_login(u, p):
            self.username = u
            self.password = p
            print "Login Success!!"
        else:
            print "there was a problem logging in"

    def view_user(self):
        if self.username is None:
            print "Please Login First!"

        else:
            print (self.username, self.password)



app = App()



menu = """

1. Login
2. Register
3. View User """

print menu 
while True:
    {'2':create_username,'l':app.login, '3' :app.view_user}.get(raw_input("Chosee with number (1,2,3: ")).lower(),app.login()

I combine this code, or using from u code, still won't work, invalid syntaz brother. Sorry If i'm bothering.

This article has been dead for over six months. Start a new discussion instead.