python 练习题

长方形

height = int(input("please you want to height:"))
width = int(input("please you want to "))
num_height = 0
while height > num_height:
    num_width = 1
    while width > num_
        num_width += 1
        print("*",end="")
    print("*")
    num_height += 1
View Code

乘法表

num = 1
while num <= 9:
    every = 1
    while num >= every:
        print(str(num) + "*" + str(every) + " = " + str(num*every),end="  ")
        every += 1
    print()
    num += 1
View Code

用户注册登陆

information = {}
def information_function():
    sign_error_password = False
    while True:
        sign_register_in = False
        if sign_error_password == False:
            register = input("是否进行注册(y/n):")
            if register == "y":
                sign_register_in = True
            elif register == "q":
                break
        number = input("账号:")
        password = input("密码:")
        sign_error_password = False
        if number in information and sign_register_in == True:
            print("当前账户已经存在")
        elif len(password) <= 6 and sign_register_in == True:
            print(len(password))
            print("密码至少输入6位")
        else:
            if sign_register_in == True:
                information[number] = password
            if number in information and information[number] == password:
                print("Welcome")
            else:
                print("账户或密码错误!")
                sign_error_password = True
information_function()
View Code

三级菜单

menu = {"山东":{"济南":{"历下区",
                    "市中区",
                    "愧荫区"},
              "青岛":{"李沧区",
                    "市南区",
                    "城阳区"},
              "淄博":{"淄博区",
                    "张店区",
                    "博山区"}},
        "浙江":{"杭州":{"上城区",
                    "下城区",
                    "江干区"},
              "宁波":{"海曙区",
                   "江北区",
                   "镇海区",}}}
current_layer = menu
current_sign = []
while True:
    for key in current_layer:
        print(key)
    response = input(">>>")
    if len(response) == 0:continue
    if response in current_layer:
        current_sign.append(current_layer)
        current_layer = current_layer[response]
        print(current_layer)
    elif response == "b":
        if current_sign:
            current_layer = current_sign[-1]
            current_sign.pop()
    else:
        print("无此项
-------------")
View Code

装饰器的运用

jd_user,jd_passwd = "123","321"
wx_user,wx_passwd = "111","222"
jd_log = False
wx_log = False
def sign(auth):
    def mark(func):
        def log():
            global jd_log,wx_log
            if auth == "jd":
                if jd_log == False:
                    jd_u = input("JD_Username:")
                    jd_p = input("Password:")
                    if jd_u == jd_user and jd_p == jd_passwd:
                        jd_log = True
                        func()
                    else:
                        print("you input username or password error!")
                elif jd_log == True:
                    func()
            else:
                if wx_log == False:
                    wx_u = input("WX_Username:")
                    wx_p = input("Password:")
                    if wx_u == wx_user and wx_p == wx_passwd:
                        wx_log = True
                        func()
                    else:
                        print("you input username or password error!")
                elif wx_log == True:
                    func()
        return log
    return mark

@sign("jd") #    home = sign("jd") = mark = mark(log)
def home(): # jd
    print("welcome home")

@sign("wx")
def finance(): # wx
    print("welcome finance")

@sign("jd")
def book(): #jd
    print("welcome book")

if __name__ == "__main__":
    item = list(["1、home",
          "2、finance",
          "3、book"])
    for i in item:
        print(i)
    while True:
        choose = input(">>>")
        if choose == "1":
            home()
        elif choose == "2":
            finance()
        elif choose == "3":
            book()
        elif choose == "1":
            break
        else:
            print("you input error!")
View Code
原文地址:https://www.cnblogs.com/py-peng/p/10737749.html