写一个图书馆的例子

users={"wulaoshi":"123456"}
login_users={}
books={}
def register():
    global users
    while 1:
        username = input("请输入您要注册的用户名:")
        if not len(username) >= 7:
            print("您输入的用户名少于7位,请重新输入")
            continue
        for i in username:
            if not (i>="a" and i<="z") and i!="_":
                print("您输入的用户名必须是小写字母或_!")
                break
        else: 
            if username in users:
                print("您输入的用户名已经存在,请重新输入!")
                continue
        break
    while 1:
        password = input("请输入您要使用的密码:")
        if not len(password) >= 7:
            print("您输入的密码少于7位,请重新输入")
            continue
        for i in password:
            if not (i>="a" and i<="z") and i!="_" and not (i>="0" and i<="9") :
                print("您输入的密码必须是小写字母、数字或_!")
                break
        else:
            password2 = input("请再次输入您要使用的密码:")
            if password==password2:
                users[username]=password
                print("%s 注册已经成功!" %username)
                break
            else:
                print("两次输入的密码不一致!请重新输入!")
  
def login():
    global users
    while 1:
        login_username= input("请输入登录的用户名:")
        login_password=input("请输入登录的密码:")
        if login_username in users:
            if login_password==users[login_username]:
                print("%s 用户登录成功!" %login_username)
                login_users[login_username]=None
                break        
        print("您输入的用户名和密码有误,请重新输入!")

def addbook():
    global books
    bookid = input("请输入新增图书的图书id:")
    if bookid not in books.keys():
        for i in bookid:
            if not (i>="0" and i<="9"):
                print("输入的图书id不是纯数字,请重新输入")
                break
        else:
            book_name = input("请输入新增图书的名字:")
            if book_name.strip!="":
                books[bookid]=book_name
                print("添加图书《%s》成功!" %book_name)

def listbooks():
    for k,v in books.items():
        print("图书编号%s:%s" %(k,v))
            
#register() 
#login()

print("欢迎使用光荣之路图书馆系统:")
print("""
    命令清单:
    注册用户请输入:register
    登录请输入:login
    添加新书:addbook
    查看所有书籍:listbooks
""")
while 1:
    command = input("请您输入要操作的命令:")
    if not command in ["register","login","addbook","listbooks"]:
        print("您输入的命令不存在,请重新输入!")
        continue
    exec(command+"()")

猜你喜欢

转载自www.cnblogs.com/wenm1128/p/11619661.html