购物小编程(完整编码)

goods = [{"name": "电脑", "price": 1999},
         {"name": "鼠标", "price": 10},
         {"name": "游艇", "price": 20},
         {"name": "美女", "price": 998}, ]
要求:
# 1:页面显示序号 + 商品名称 + 商品价格,如:1.电脑 1999 2 鼠标 10 …
# 2:用户输入选择的商品序号,然后打印商品名称及商品价格
# 3:如果用户输入的商品序号有误,则提示输入有误,并重新输入。
# 4:用户输入Q或者q,退出程序。

goods = [{"name": "电脑", "price": 1999}, {"name": "鼠标", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ] for new_input in range(len(goods)): print("显示序号%s\t\ 商品名称%s\t 商品价格%s\t" %(new_input+1,goods[new_input]['name'],goods[new_input]['price'])) while 1: number = input("请输入序号(输入 q 或 Q 退出)") if number.isdigit(): num = int(number) if num in range(1,len(goods)+1): print("显示序号%s\t 商品名称%s\t 商品价格%s\t" % (num , goods[num-1]['name'], goods[num-1]['price'])) else: print("输入序号有误") continue elif number == "q" or number =='Q': print("退出程序") break else: print("输入有误,请再次输入") continue

  

猜你喜欢

转载自www.cnblogs.com/python119/p/9403941.html
今日推荐