项目 | 价格 |
---|---|
Mac | ¥9000 |
Kindle | ¥800 |
Tesla | ¥900000 |
Python Book | ¥105 |
Bike | 2000 |
答案
product_list = [
('Mac', 9000),
('Kindle', 800),
('tesla', 900000),
('python book', 105),
('bike', 2000)
]
saving = input('please input your saving:')
shopping_cart = []
# 验证是否是数字
if saving.isdigit():
saving = int(saving)
# for i in product_list:
# print(product_list.index(i), i)
for i,k in enumerate(product_list):
print(i+1, '>>>', k)
while True:
choice = input('选择商品的编号[退出:q]:')
if choice.isdigit():
choice = int(choice)
if choice > 0 and choice < len(product_list):
p_item = product_list[choice-1]
if p_item[1] < saving:
saving -= p_item[1]
shopping_cart.append(p_item[0])
print(f'当前购物车内已有物品为{shopping_cart},余额为{saving}')
else:
print(f'余额不足,还剩{saving}元')
elif choice == 'q':
print('---您的购物车里有如下商品---')
for i in shopping_cart:
print(i)
print(f'你的余额为{saving}元')
break
else:
print('invalid input')
menu = {
'北京':{
'朝阳':{
'CICC':{
1}, 'HP':{
2}},
'望京':{
'MOMO':{
3}, '360':{
4}},
'三里屯':{
'优衣库':{
5}, 'apple':{
6}}
},
'河北':{
}
}
# 利用标志位来退出
back_flag = False
exit_flag = False
while not back_flag and not exit_flag:
for key in menu:
print(f'子菜单1:{key}')
print('当前为第1页 / 退出输入q')
choice = input(">:").strip()
if choice == 'q':
exit_flag = True
break
elif choice in menu:
while not back_flag and not exit_flag:
for key2 in menu[choice]:
print(f'子菜单2:{key2}')
print('当前为第2页,返回上一页请输入a / 退出输入q')
choice2 = input(">>").strip()
if choice2 == 'a':
back_flag = True
elif choice2 == 'q':
exit_flag = True
elif choice2 in menu[choice]:
while not back_flag and not exit_flag:
for key3 in menu[choice][choice2]:
print(f'子菜单3:{key3}')
print('当前为第3页,返回上一页输入a / 返回上两页输入b / 退出输入q')
choice3 = input(">>>").strip()
if choice3 == 'a':
back_flag = True
elif choice3 == 'q':
exit_flag = True
elif choice3 in menu[choice][choice2]:
while not back_flag and not exit_flag:
print('---End Level---')
print('当前为第4页,返回上一页输入a / 返回上两页输入b / 退出输入q')
choice4 = input('>>>>').strip()
if choice4 == 'a':
back_flag = True
elif choice4 == 'q':
exit_flag = True
else:
print('invalid input')
else:
back_flag = False
else:
print("invalid True")
else:
back_flag = False
else:
print('invalid input')
else:
back_flag = False
else:
print('invalid input')
menu = {
'北京':{
'朝阳':{
'CICC':{
1}, 'HP':{
2}},
'望京':{
'MOMO':{
3}, '360':{
4}},
'三里屯':{
'优衣库':{
5}, 'apple':{
6}}
},
'河北':{
}
}
current_layer = menu
parent_layer = []
print('输入a返回上一层,输入q退出')
while True:
for key in current_layer:
print(key)
choice = input('>>>:').strip()
if len(choice) == 0:
continue
if choice in current_layer:
parent_layer.append(current_layer)
current_layer = current_layer[choice]
elif choice == 'a':
if len(parent_layer) > 0:
current_layer = parent_layer.pop()
layer_th -= 1
else:
print('This is the Top Level!')
elif choice == 'q':
break
else:
print('valid input')