不能笑却又忍不住(low到不能行)
# 30、购物车
# 功能要求:要求用户输入总资产,
# 例如: 2000显示商品列表,让用户根据序号选择商品,加入购物车购买,
# 如果商品总额大于总资产,提示账户余额不足,
# 否则,购买成功
goods=[
{"name":"电脑","price":1999},
{"name":"鼠标","price":10},
{"name":"游艇","price":20},
{"name":"美女","price":998},]
number = '序号|商品|价格'
while True:
x = 1
list1 = []
sum1 = 0
total = input('请输入你的总资金:')
if total.isdigit():
if int(total) >= 10: # 小于10元退出
while True:
print(number)
for i in range(1, 5): # 循环打印商品和价格
print(i, ' ', goods[i - 1]['name'], goods[i - 1]['price'])
choice = input('请输入你选择的商品序号')
if choice.isdigit():
choice = int(choice)
if 1 <= choice <= 4:
if x == 1: # 首次买商品
if goods[choice - 1]['price'] < int(total):
sum1 += goods[choice - 1]['price']
print(goods[choice - 1]['name'], '已经加入购入车您还剩', (int(total) - sum1), '元钱您还需要买什么?'
'输入Q退出并成功购买。')
list1.append(goods[choice - 1]['name'])
else:
print('余额不足,不能购买')
x = 0
if x > 1: # 第二次买商品
if goods[choice - 1]['price'] > int(total) - sum1:
print('余额不足,不能购买', goods[choice - 1]['name'])
else:
sum1 += goods[choice - 1]['price']
print(goods[choice - 1]['name'], '已经加入购入车您还剩', (int(total) - sum1), '元钱您还需要买什么?'
'输入Q退出并成功购买。')
list1.append(goods[choice - 1]['name'])
x += 1
else:
print('输入有误')
else:
if choice.upper() == 'Q':
if bool(list1) == False:
print('你啥都没买啊!')
break
else:
print('您成功购买:', list1)
break
else:
print('输入错误')
else:
print('你啥都买不起啊,退下吧!')
break
else:
if total.upper() == 'Q':
break
else:
print('输入错误')
快递寄回家(呵呵了哈,200行 VS 12 行代码)
def menu_2(menu):
while True:
for i in menu:
print(i)
choice = input('选择:')
if choice == 'q' or choice == 'b':
return choice
elif choice in menu and menu[choice]:
borq = menu_2(menu[choice])
if borq == 'q':
return 'q'
menu_2(menu)
====================================================================================
# 关键词:while(循环),choice(选择),flag(标示),menu(菜单),key(键),break,strip
# 功能要求:见截图三级菜单
menu = {
'北京':{
'海淀':{
'五道口':{
'sogo':{},
'网易':{},
'google':{}
},
'中关村':{
'爱奇艺':{},
'汽车之家':{},
'youku':{},
},
'上地':{
'百度':{},
},
},
'昌平':{
'沙河':{
'汇德商厦老男孩':{},
'北航':{},
},
'天通苑':{},
'回龙观':{},
},
'朝阳':{
'朝阳大妈' },
'东城':{},
},
'上海':{
'闵行':{
"人民广场":{
'炸鸡店':{}
}
},
'闸北':{
'火车站':{
'携程':{}
}
},
'浦东':{},
},
'山东':{
'济南':{}
},
}
flag = True
while flag:
for i in menu:
print(i)
choice1 = input('请选择输入1(q退出b返回)')
if choice1.upper() == 'Q':
flag = False
elif choice1.upper() == 'B':
break
elif choice1 == '北京':
while flag:
for j in menu['北京']:
print(j)
choice2 = input('请选择输入2(q退出b返回)')
if choice2.upper() == 'Q':
flag = False
elif choice2.upper() == 'B':
break
elif choice2 == '海淀':
while flag:
for k in menu['北京']['海淀']:
print(k)
choice3 = input('请选择输入3(q退出b返回)')
if choice3.upper() == 'Q':
flag = False
elif choice3.upper() == 'B':
break
elif choice3 == '五道口':
while flag:
for l in menu['北京']['海淀']['五道口']:
print(l)
choice4 = input('请选择输入4(q退出b返回)')
if choice4.upper() == 'Q':
flag = False
elif choice4.upper() == 'B':
break
elif choice4 == 'sogo':
print('您选择的地址是:',choice1,'-',choice2,'-',choice3,'-',choice4)
flag = False
elif choice4 == 'Google':
print('您选择的地址是:',choice1,'-',choice2,'-',choice3,'-',choice4)
flag = False
elif choice4 == '网易':
print('您选择的地址是:',choice1,'-',choice2,'-',choice3,'-',choice4)
flag = False
elif choice3 == '中关村':
while flag:
for l in menu['北京']['海淀']['中关村']:
print(l)
choice4 = input('请选择输入4(q退出b返回)')
if choice4.upper() == 'Q':
flag = False
elif choice4.upper() == 'B':
break
elif choice4 == '爱奇艺':
print('您选择的地址是:', choice1, '-', choice2, '-', choice3, '-', choice4)
flag = False
elif choice4 == '汽车之家':
print('您选择的地址是:', choice1, '-', choice2, '-', choice3, '-', choice4)
flag = False
elif choice4 == 'youku':
print('您选择的地址是:', choice1, '-', choice2, '-', choice3, '-', choice4)
flag = False
elif choice3 == '上地':
while flag:
for l in menu['北京']['海淀']['上地']:
print(l)
choice4 = input('请选择输入4(q退出b返回)')
if choice4.upper() == 'Q':
flag = False
elif choice4.upper() == 'B':
break
elif choice4 == '百度':
print('您选择的地址是:', choice1, '-', choice2, '-', choice3, '-', choice4)
flag = False
elif choice2 == '昌平':
while flag:
for p in menu['北京']['昌平']:
print(p)
choice3 = input('请选择输入3(q退出,b返回)')
if choice3.upper() == 'Q':
flag = False
elif choice3.upper() == 'B':
break
elif choice3 == '沙河':
while flag:
for o in menu['北京']['昌平']['沙河']:
print(o)
choice4 = input('请选择输入4(q退出,b返回')
if choice4.upper() == 'Q':
flag = False
elif choice4.upper() == 'B':
break
elif choice4 == '汇德商厦老男孩':
print('您选择的地址是:', choice1, '-', choice2, '-', choice3, '-', choice4)
flag = False
elif choice4 == '北航':
print('您选择的地址是:',choice1,'-',choice2,'-',choice3,'-',choice4)
flag = False
elif choice3 == '天通苑':
print('您选择的地址是:', choice1, '-', choice2, '-', choice3)
flag = False
elif choice3 == '回龙观':
print('您选择的地址是:', choice1, '-', choice2, '-', choice3)
flag = False
elif choice2 == '朝阳':
while flag:
for m in menu['北京']['朝阳']:
print(m)
choice3 = input('请选择输入3(q退出,b返回')
if choice3.upper() == 'Q':
flag = False
elif choice3.upper() == 'B':
break
elif choice3 == '朝阳大妈':
print('您选择的地址是:', choice1, '-', choice2, '-', choice3)
flag = False
elif choice2 == '东城':
print('您选择的地址是:', choice1, '-', choice2)
flag = False
elif choice1 == '上海':
while flag:
for n in menu['上海']:
print(n)
choice2 = input('请选择输入3(q退出,b返回')
if choice2.upper() == 'Q':
flag = False
elif choice2.upper() == 'B':
break
elif choice2 == '闵行':
while flag:
for x in menu['上海']['闵行']:
print(x)
choice3 = input('请选择输入3(q退出,b返回)')
if choice3.upper() == 'Q':
flag = False
elif choice3.upper() == 'B':
break
elif choice3 == '人民广场':
while flag:
for l in menu['上海']['闵行']['人民广场']:
print(l)
choice4 = input('请选择输入4(q退出,b返回)')
if choice4.upper() =='Q':
flag = False
elif choice4 .upper() == 'B':
break
elif choice4 == '炸鸡店':
print('您选择的地址是:',choice1,'-',choice2,'-',choice3,'-',choice4)
flag = False
elif choice2 == '闸北':
while flag:
for z in menu['上海']['闸北']:
print(z)
choice3 = input('请选择输入3(q退出,b返回)')
if choice3.upper() == 'Q':
flag = False
elif choice3.upper() == 'B':
break
elif choice3 == '火车站':
while flag:
for v in menu['上海']['闸北']['火车站']:
print(v)
choice4 = input('请选择输入4(q退出,b返回)')
if choice4.upper() =='Q':
flag = False
elif choice4 .upper() == 'B':
break
elif choice4 == '携程':
print('您选择的地址是:',choice1,'-',choice2,'-',choice3,'-',choice4)
flag = False
elif choice2 == '浦东':
print('您选择的地址是:', choice1, '-', choice2)
flag = False
elif choice1 == '山东':
while flag:
for b in menu['山东']:
print(b)
choice2 = input('请选择输入2(q退出,b返回)')
if choice2.upper() == 'Q':
flag = False
elif choice2.upper() == 'B':
break
elif choice2 == '济南':
print('您选择的地址是:', choice1, '-', choice2)
flag = False
else:
print('请输入正确地址!')