2018年6月日周末作业

1,购物车

功能要求:
要求用户输入自己拥有总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
]
user_money = input("请输入您的资产>>")
user_money = int(user_money)
for item in goods:
    print(goods.index(item) + 1, item['name'], item['price'])
car = []
sum = 0
while 1:
    user_section = input("请输入您的选择的商品序号>>")

    if user_section.upper() == 'Q':

        if len(car) != 0:
            print("恭喜您购买成功,您一共消费%s元" % sum)
        break
    if user_section.isdigit() and int(user_money) >= 0 and int(user_section) <= len(goods):
        if user_money < sum:
            print('商品价格为%s元,您的余额不足' % sum)
            break
        user_section = int(user_section) - 1
        car.append(goods[user_section]['name'])
        sum += goods[user_section]['price']
    else:
        print("非法输入")
View Code

2,计算BIM

# 1.创建并输出菜单, 菜单是不可变的. 所以使用元组

# 存储用户的信息 id: {'name':'名字', 'weight':体重, 'height':身高}
# 例如:目前有两个用户信息:1. 汪峰, 2. 章子怡
# 存储结构:
# {
# 1:{'name':'汪峰', 'weight':80, 'height':1.8, 'BMI':24.7},
# 2:{'name':'章子怡', 'weight':50, 'height':1.65, 'BMI':18.4}
# }

# 编号从1开始

# 体质指数(BMI)= 体重(kg)÷ (身高(m) x 身高(m))
# 体重的单位: KG
# 身高的单位: m
# 需求:首先。打印菜单,然后用户输入选择的菜单项
# 输入1:进入录入环节。用户需要录入:名字,身高,体重. while
# 由程序计算出BMI指数. 保存到bodies字典中. 第一个用户的id是1, 第二个是2, 以此类推
# 录入完毕后. 提示用户是否继续录入. 如果选择是, 则继续进行录入, 直到用户输入否. 则返回到主菜单
# 输入2: 进入查询环节, 提示用户输入要查询的人的id. 如果不存在,给与提示, 如果存在. 则显示出该用户的全部信息(名字,身高,体重,BMI)
# 然后提示用户是否继续查询. 如果选择是, 继续进行查询, 直到用户输入否, 返回主菜单
# 输入3: 进入删除环节, 提示用户输入要删除的人的id, 如果id不存在, 给与提示, 如果存在, 则执行删除操作. 并提示删除成功.
# 然后提示用户是否继续删除, 如果是, 继续让用户选择要删除的id, 直到用户输入否, 返回主菜单
# 输入4: 进入修改环节, 首先让用户输入要修改的人的id, 根据id查找用户信息, 如果不存在, 给与提示, 如果存在, 将用户原信息进行打印,
# 然后提示用户输入新的名字, 身高, 体重. 由程序重新计算BMI指数. 并将新的信息保存在bodies中. 同时给用户展示新的用户信息
# 然后提示用户是否继续修改, 如果是, 则继续要求用户输入id信息. 直到用户输入否, 返回主菜单.
# 输入5: 程序退出.
# 输入其他任何内容. 都予以提示不合法. 让用户重新进行输入
menus = ("1, 录入", "2, 查询", "3, 删除", "4, 修改", "5, 退出")
for item in menus:
    print(item)
bodies = {}
body_id = 1
flag = False
while 1:
    while flag:
        user_section_a = input('是/否')
        if user_section_a == '':
            dic = {"name": None, "weight": None, "height": None, "BMI": None}
            user_input_name = input('请输入您的名字>>')
            while 1:
                user_input_high = input('请输入您的身高>>')
                if not user_input_high.isdigit():
                    print('请输入正确的身高')
                else:
                    break
            user_input_high = int(user_input_high)
            while 1:
                user_input_weight = input('请输入您的体重>>')
                if not user_input_weight.isdigit():
                    print("请输入正确的体重")
                else:
                    break
            user_input_weight = int(user_input_weight)
            BMI = user_input_weight / (user_input_high ** 2)
            dic['name'] = user_input_name
            dic['height'] = user_input_high
            dic['weight'] = user_input_weight
            dic['BMI'] = BMI
            bodies[body_id] = dic
            body_id += 1
        elif user_section_a == '':
            print(bodies)
            flag = False
        else:
            print("请按照规定输入")
    user_section = input('please enter your section :')
    if user_section == '5':
        break
    if user_section == '1':
        flag = True
        dic = {"name": None, "weight": None, "height": None, "BMI": None}
        user_input_name = input('请输入您的名字>>')
        while 1:
            user_input_high = input('请输入您的身高>>')
            if not user_input_high.isdigit():
                print('请输入正确的身高')
            else:
                break
        user_input_high = int(user_input_high)
        while 1:
            user_input_weight = input('请输入您的体重>>')
            if not user_input_weight.isdigit():
                print("请输入正确的体重")
            else:
                break
        user_input_weight = int(user_input_weight)
        BMI = user_input_weight / (user_input_high ** 2)
        dic['name'] = user_input_name
        dic['height'] = user_input_high
        dic['weight'] = user_input_weight
        dic['BMI'] = BMI
        bodies[body_id] = dic
        body_id += 1
    elif user_section == '2':
        while 1:
            user_cha = input('please enter your look id>>')
            if not user_cha.isdigit():
                print('输入错误,请重新输入')
            else:
                break
        flag1 = True
        if int(user_cha) in bodies:
            print(bodies[int(user_cha)])
        else:
            print('NO the info')
        while flag1:
            user_section_b = input('是/否')
            if user_section_b == '':
                user_cha = input('please enter your look id>>')
                if int(user_cha) in bodies:
                    print(bodies[int(user_cha)])
                else:
                    print('NO the info')
            elif user_section_b == '':
                break
            else:
                print("请按照规定输入")
    elif user_section == '3':
        for item in bodies:
            print(item, bodies[item])
        while 1:
            user_shan = input('please enter your pop id')
            if not user_shan.isdigit():
                print("非法输入,请重新输入")
            else:
                break
        user_shan = int(user_shan)
        # bodies.pop(user_shan)
        if user_shan in bodies:
            bodies.pop(user_shan)
            body_id -= 1
            for key, value in bodies.items():
                if user_shan < key:
                    a, b = key - 1, value
                    del bodies[key]
                    bodies[a] = b
                else:
                    pass
        else:
            print("NO the id info")
        while 1:
            user_section_shan = input("是/否")
            if user_section_shan == "":
                for item in bodies:
                    print(item, bodies[item])
                user_shan = input('please enter your pop id')
                user_shan = int(user_shan)
                if user_shan in bodies:
                    bodies.pop(user_shan)
                    body_id -= 1
                    for key, value in bodies.items():
                        if user_shan < key:
                            a,b = key -1 , value
                            del bodies[key]
                            bodies[a] = b
                        else:
                            pass
                else:
                    print('NO the id info')
            elif user_section_shan == '':
                break
            else:
                print("请按照规定输入")
    elif user_section == '4':
        for item in bodies:
            print(item, bodies[item])
        while 1:
            user_section_gai = input('please enter you change id>>')
            if not user_section_gai.isdigit():
                print("非法输入,请重新输入")
            else:
                break
        user_section_gai = int(user_section_gai)
        if user_section_gai in bodies:
            print('您要求改的内容为%s' % bodies[user_section_gai])
            print('请输入修改之后的信息')
            gai_name = input("Name:")
            while 1:
                gai_high = input("High:")
                if not gai_high.isdigit():
                    print('非法输入,请重新输入')
                else:
                    break
            gai_high = int(gai_high)
            while 1:
                gai_weight = input("weight:")
                if not gai_weight.isdigit():
                    print("非法输入,请重新输入")
                else:
                    break
            gai_weight = int(gai_weight)
            gai_BMI = gai_weight/(gai_high**2)
            bodies[user_section_gai] = {'name': gai_name, 'weight': gai_weight, 'high': gai_high, 'BMI': gai_BMI}
        else:
            print("NO the id info")
        while 1:
            user_section_gai_a = input('是/否')
            if user_section_gai_a == '':
                while 1 :
                    user_section_gai = input('please enter you change id>>')
                    if not user_section_gai.isdigit():
                        print('非法输入,请重新输入')
                    else:
                        break
                user_section_gai = int(user_section_gai)
                if user_section_gai in bodies:
                    print('您要求改的内容为%s' % bodies[user_section_gai])
                    print('请输入修改之后的信息')
                    gai_name = input("Name:")
                    while 1:
                        gai_high = input("High:")
                        if not gai_high.isdigit():
                            print("非法输入,请重新输入")
                        else:
                            break
                    gai_high = int(gai_high)
                    while 1:
                        gai_weight = input("weight:")
                        if not gai_weight.isdigit():
                            print("非法输入,请重新输入")
                        else:
                            break
                    gai_weight = int(gai_weight)
                    gai_BMI = gai_weight / (gai_high ** 2)
                    bodies[user_section_gai] = {'name': gai_name, 'weight': gai_weight, 'high': gai_high,
                                                'BMI': gai_BMI}
                else:
                    print("NO the id info")
            elif user_section_gai_a == '':
                break
            else:
                print('请按照规定输入')
    else:
        print('输入错误')
View Code
 
 
 

猜你喜欢

转载自www.cnblogs.com/chenrun/p/9159122.html