这个程序是唯一一次不看老师代码,自己敲出来的,敲出来后还是有报错的,但是以自己现在的能力水平自己不知道如何改进,也希望各位网友可以提出来,谢谢。其实老师的程序也不是面面俱到,也是有很多的bug,但是可以运行下来,里边的一些写脚本思路也是可以借鉴的。

原始程序:

#!/usr/bin/env python

register = {}

def newuser():
    while True:
        username = raw_input("username: ")
        if register.get(username, "exist,try again"):
            break
        pwd = raw_input("password: ")
        register = {username:pwd }
        print "register ok!!!"

def olduser():
    while True:
        username = raw_input("username: ")
        if not register.get(username):
            print "not exist, try again"
            break
        pwd = raw_input("password: ")

        if register(username) == pwd:
            print "login successfll"


CMDs = {"1":newuser, "2":olduser}

def main():
    prompt = """(1)new user register
(2)olduser loging
(3)quit
please input your choice(1/2/3): """
    while True:
    choice = raw_input(prompt).strip()[0]
        if choice not in "123":
            print "Invalid choice,try again"
            continue
        if choice == "3":
            break
        CMDs[choice]()

if __name__ == "__main__":
    main()

以写代学: python 模拟用户注册或登录账号_模拟

老师程序

#!/usr/bin/env python

import getpass

user_dict = {}

def new_user():
    username = raw_input("username: ")
    if username not in user_dict:
        password = raw_input("password: ")
        user_dict[username] = password
def old_user():
    username = raw_input("username: ")
    password = getpass.getpass("password: ")
    if user_dict.get(username) == password:
        print "Login successful!"
    else:
        print "Login incorrect."

CMDs = {"0":new_user, "1":old_user}
def show_menu():
    prompt = """(0)register user
(1)user login
(2)quit
Please input your choice(0/1/2): """
    while True:
        choice = raw_input(prompt).strip()[0]
        if choice not in "012":
            print "Invalid choice. Try agin."
            continue
        if choice == "2":
            break
        CMDs[choice]()
        
if __name__ == "__main__":
    show_menu()