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)breakif user_section.isdigit() and int(user_money) >= 0 and int(user_section) <= len(goods):if user_money < sum:print('商品價格為%s元,您的余額不足' % sum)breakuser_section = int(user_section) - 1car.append(goods[user_section]['name'])sum += goods[user_section]['price']else:print("非法輸入")
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:breakuser_input_high = int(user_input_high)while 1:user_input_weight = input('請輸入您的體重>>')if not user_input_weight.isdigit():print("請輸入正確的體重")else:breakuser_input_weight = int(user_input_weight)BMI = user_input_weight / (user_input_high ** 2)dic['name'] = user_input_namedic['height'] = user_input_highdic['weight'] = user_input_weightdic['BMI'] = BMIbodies[body_id] = dicbody_id += 1elif user_section_a == '否':print(bodies)flag = Falseelse:print("請按照規定輸入")user_section = input('please enter your section :')if user_section == '5':breakif user_section == '1':flag = Truedic = {"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:breakuser_input_high = int(user_input_high)while 1:user_input_weight = input('請輸入您的體重>>')if not user_input_weight.isdigit():print("請輸入正確的體重")else:breakuser_input_weight = int(user_input_weight)BMI = user_input_weight / (user_input_high ** 2)dic['name'] = user_input_namedic['height'] = user_input_highdic['weight'] = user_input_weightdic['BMI'] = BMIbodies[body_id] = dicbody_id += 1elif user_section == '2':while 1:user_cha = input('please enter your look id>>')if not user_cha.isdigit():print('輸入錯誤,請重新輸入')else:breakflag1 = Trueif 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 == '否':breakelse: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:breakuser_shan = int(user_shan)# bodies.pop(user_shan)if user_shan in bodies:bodies.pop(user_shan)body_id -= 1for key, value in bodies.items():if user_shan < key:a, b = key - 1, valuedel bodies[key]bodies[a] = belse:passelse: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 -= 1for key, value in bodies.items():if user_shan < key:a,b = key -1 , valuedel bodies[key]bodies[a] = belse:passelse:print('NO the id info')elif user_section_shan == '否':breakelse: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:breakuser_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:breakgai_high = int(gai_high)while 1:gai_weight = input("weight:")if not gai_weight.isdigit():print("非法輸入,請重新輸入")else:breakgai_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:breakuser_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:breakgai_high = int(gai_high)while 1:gai_weight = input("weight:")if not gai_weight.isdigit():print("非法輸入,請重新輸入")else:breakgai_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 == '否':breakelse:print('請按照規定輸入')else:print('輸入錯誤')
?這個是修改版本,上面那個有瑕疵


menus = ("1, 錄入", "2, 查詢", "3, 刪除", "4, 修改", "5, 退出")bodies = {} body_id = 1while 1:for item in menus:print(item)user_section = input('please enter your section :')if user_section == '1':flag = Truewhile flag:dic = {"name": None, "weight": None, "height": None, "BMI": None}user_input_name = input('請輸入您的名字>>')while 1:user_input_high = input('請輸入您的身高>>')if not user_input_high.startswith(".") and not user_input_high.endswith(".") and user_input_high.count(".") == 1:breakelse:print("請輸入正確的格式")user_input_high = float(user_input_high)while 1:user_input_weight = input('請輸入您的體重>>')if not user_input_weight.startswith(".") and not user_input_weight.endswith(".") and user_input_weight.count(".") == 1 or user_input_weight.isdigit():breakelse:print("請輸入正確的格式")user_input_weight = float(user_input_weight)BMI = user_input_weight / (user_input_high ** 2)dic['name'] = user_input_namedic['height'] = user_input_highdic['weight'] = user_input_weightdic['BMI'] = BMIbodies[body_id] = dicbody_id += 1while 1:user_section_a = input('是/否')if user_section_a == '是':breakelif user_section_a == '否':print(bodies)flag = Falsebreakelse:print("請按照規定輸入")elif user_section == '2':flag2 = Truewhile flag2:user_cha = input('please enter your look id>>')if not user_cha.isdigit():print('輸入錯誤,請重新輸入')else:if int(user_cha) in bodies:print(bodies[int(user_cha)])else:print('NO the info')while 1:user_section_b = input('是/否')if user_section_b == '是':breakelif user_section_b == '否':flag2 = Falsebreakelse:print("請按照規定輸入")elif user_section == '3':flag3 = Truewhile flag3:while 1:user_shan = input('please enter your pop id')if not user_shan.isdigit():print("非法輸入,請重新輸入")else:breakuser_shan = int(user_shan)if user_shan in bodies:bodies.pop(user_shan)body_id -= 1for key, value in bodies.items():if user_shan < key:a, b = key - 1, valuedel bodies[key]bodies[a] = belse:passelse:print("NO the id info")user_section_b = input('是/否')if user_section_b == '是':breakelif user_section_b == '否':flag3 = Falsebreakelse:print("請按照規定輸入")elif user_section == '4':flag4 = Truewhile flag4:while 1:user_section_gai = input('please enter you change id>>')if not user_section_gai.isdigit():print("非法輸入,請重新輸入")else:breakuser_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.startswith(".") and not gai_high.endswith(".") and gai_high.count(".") == 1:breakelse:print("輸入非法")gai_high = float(gai_high)while 1:gai_weight = input("weight:")if not gai_weight.startswith(".") and not gai_weight.endswith(".") and gai_weight.count(".") == 1 or gai_weight.isdigit():breakelse:print("非法輸入")gai_weight = float(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_b = input('是/否')if user_section_b == '是':breakelif user_section_b == '否':flag4 = Falsebreakelse:print("請按照規定輸入")elif user_section == '5':breakelse:print('非法輸入')
?