python 购物车和三级菜单

程序:购物车程序

需求:

  1. 启动程序后,让用户输入工资,然后打印商品列表
  2. 允许用户根据商品编号购买商品
  3. 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
  4. 可随时退出,退出时,打印已购买商品和余额
#-*- coding:utf8 -*-
shopping_list=[]
product_list = [
('Iphone',5800),
('Mac Pro',9800),
('Bike',800),
('Watch',10600),
('Coffee',31),
('Alex Python',120),
]
salary = input("Input your salary:")
if salary.isdigit():
salary=int(salary)
while True:
for index ,item in enumerate(product_list):
print(index,item)
user_choice=input("请选择您要买的商品:")
if user_choice.isdigit():
user_choice=int(user_choice)
if user_choice<len(product_list) and user_choice>=0:
item_choice=product_list[user_choice]
if item_choice[1]<=salary:
print ("买的起")
shopping_list.append(item_choice)
salary-=item_choice[1]
print("将您的商品%s加入购物车,余额%s" %(item_choice,salary))
else:
print("余额不够:",salary)
else:
print("商品不存在")
elif user_choice=='q':
print("--打印商品列表-----")
for i in product_list:
print(i)
print("您当前的余额:",salary)
exit()
else:
print ("不正确")

程序: 三级菜单

要求:

  1. 打印省、市、县三级菜单
  2. 可返回上一级
  3. 可随时退出程序
#-*- coding:utf8 -*-
data = {
'北京':{
"昌平":{
"沙河":["oldboy","test"],
"天通苑":["链家地产","我爱我家"]
},
"朝阳":{
"望京":["奔驰","陌陌"],
"国贸":{"CICC","HP"},
"东直门":{"Advent","飞信"},
},
"海淀":{},
}, '广东':{
"东莞":{},
"常熟":{},
"佛山":{},
},
} exit_flag=True
while exit_flag:
for i in data:
print(i)
choice=input("请选择入口:")
if choice in data:
while exit_flag:
for i2 in data[choice]:
print("\t\t",i2)
choice2=input("请选择入口2:")
if choice2 in data[choice]:
while exit_flag:
for i3 in data[choice][choice2]:
print("\t\t",i3)
choice3=input("请选择入口3:")
if choice3 in data[choice][choice2]:
for i4 in data[choice][choice2][choice3]:
print("\t\t", i4)
choice4=input("最后一层按b返回:")
if choice4=='b':
pass
if choice4=='q':
exit_flag=False
if choice3=="b":
break
elif choice3=="q":
exit_flag=False
if choice2=="b":
break
elif choice2=="q":
exit_flag=False
上一篇:python 小程序—三级菜单—循环和字典练习


下一篇:tail -f 命令暂停方法