2019-05-25

作业

users =[]
def read_username():
    username =input('输入用户名')
    if not 6<=len(username)<9:
        print('用户名长度不合法')
        return read_username()
    name_list =[user.get('username') for user in users]
    if username in name_list:
        print('已存在')
        return read_username()
    return username

def read_pwd():
    pwd =input ('输入密码1')
    if len(pwd) <6 or len(pwd)>9:
        print('不合法')
        return read_pwd()
    return pwd
def register():
    username =read_username()
    pwd = read_pwd()
    user ={}
    user['username']=username
    user ['pwd']=pwd
    users.append(user)
    print('注册成功')
    print(users)
def menu():
        while True:
            opt =input('1注册 2 登录 3 退出')
            if opt == '1':
                register()
            if opt == '2':
                pass
            if opt == '3':
                print('jieshu')
                break
def login():
    username =input('请输入用户名')
    pwd =input('输入密码')


menu()
products=[{'pname':'book','number':100,'price':8},
          {'pname':'computer','number':10,'price':5000}]
def two_discount(totalprice):
    print('三折')
    return totalprice*0.3
def three_discount(totalprice):
    print('二折')
    return totalprice*0.2
def total_price_count(price,strategy=None):
    if strategy == None:
        return price
    return strategy(price)
def app():
    total_num =sum([item['number'] for item in products])
    total_price =sum([item['price'] for item in products])
    strategy =None
    if total_num >=3:
        strategy = two_discount
    if total_price >=3000:
        strategy = three_discount
    price =total_price_count(total_price,strategy)
    print(total_price)
    print(price)
    print(strategy)
app()

你可能感兴趣的:(2019-05-25)