https://blog.csdn.net/KassadinSw/article/details/72933917?locationNum=3&fps=1
案例1:三级菜单实例
#!/usr/bin/env python
#Author ZJF
city_data = {
'重庆':{
"渝中区":["七星岗","大溪沟","李子坝"],
"南川区":["文凤镇","三泉镇","兴隆镇"],
"合川区":["金子镇","黄土镇","大石镇"],
"南岸区":["陈家湾","广阳镇","柏林村"]
},
'天津':{
"和平区":["崇仁里","三德里","泰华里"],
"河西区":["曙光里","红山里","大庆里"],
"武清区":["南蔡村镇","曹子里乡","上马台镇"],
"北辰区":["双街镇","小淀镇","青光镇"]
},
'北京':{
"东城区":["钱粮胡同","南剪子巷","晨光街"],
"丰台区":["马家堡","宣武门","白堆子"],
"海淀区":["颐和园","清华大学","北京大学"],
"通州区":["西集镇","台湖镇","甘棠镇"]
},
'上海':{
"浦东新区":["川沙镇","金桥","高桥镇"],
"闵行区":["莘庄","九宫宾馆","沁园春三村"],
"长宁区":["上海动物园","北新泾","虹桥公园"],
"黄浦区":["外滩","南京东路","十六铺"]
}
}
jump_flag = False # 用于跳出外循环
while not jump_flag:
for i in city_data:
print(i)
choice = input("请选择进入,或输入“q”退出程序:")
if choice in city_data:
while not jump_flag:
for i2 in city_data[choice]:
print("\t",i2)
choice2 = input("请选择进入,或输入“b”返回上一层,输入“q”退出程序:")
if choice2 in city_data[choice]:
while not jump_flag:
for i3 in city_data[choice][choice2]:
print("\t\t",i3)
choice3 = input("最后一层,输入“b”返回上一层,输入“q”退出程序:")
if choice3 == "b":
break
elif choice3 == "q":
jump_flag = True
elif choice2 == "b":
break
elif choice2 == "q":
jump_flag = True
elif choice == "q":
jump_flag = True
案例2:购买商品
- 要求用户输入总资产,例如:2000
- 显示商品列表,让用户根据序号选择商品,加入购物车
- 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
- 附加:可充值、某商品移除购物车
def work4():
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
# 要求用户输入总资产
total_money = int(input("小子你有多少钱啊: "))
print('''
我们有以下商品供您选择:
序号======>名称======>价格
1 ======>电脑======>1999
2 ======>鼠标======>10
3 ======>游艇======>20
4 ======>美女======>998
''' )
tag = True
while tag:
# 显示商品列表,让用户根据序号选择商品,加入购物车(dic)
dic = {}
tag1 = True
while tag1:
choose_number = int(input("请输入商品所对应的序号: \n如需充值请输入 5"))
n = choose_number
if n == 1:
print("您选择的商品为电脑,价格为1999元")
dic["1"] = 1999
print("\n")
balance = total_money-dic[str(n)]
print("账户余额: ", balance)
ex = input("如需进行支付请输入 quit,输入键进行购买")
if ex == "quit":
tag1 = False
continue
elif n == 2:
print("您选择的商品为鼠标,价格为10元")
dic["2"] = 10
print("\n")
balance = total_money-dic[str(n)]
print("账户余额: ", balance)
ex = input("如需进行支付请输入 quit,输入任意键进行购买")
if ex == "quit":
tag1 = False
continue
elif n == 3:
print("您选择的商品为游艇,价格为20元")
dic["3"] = 20
print("\n")
balance = total_money-dic[str(n)]
print("账户余额: ", balance)
ex = input("如需进行支付请输入 quit,输入任意键进行购买")
if ex == "quit":
tag1 = False
continue
elif n == 4:
print("您选择的商品为美女,价格为998元")
dic["4"] = 998
print("\n")
balance = total_money-dic[str(n)]
print("账户余额: ", balance)
ex = input("如需进行支付请输入 quit")
if ex == "quit":
tag1 = False
continue
elif n == 5:
money = int(input("你要充值多少钱: "))
total_money += money
balance = total_money
print("账户余额: ", balance)
ex = input("如需进行支付请输入 quit,输入mai键进行购买")
if ex == "quit" or "mai":
tag1 = False
continue
cut_hand = int(input("请输入 3 进行购买: \n输入6进行删除购物车"))
if cut_hand == 3:
if total_money >= dic[str(n)]:
print("购买成功")
else:
print("you are a low b,please choose other cheap thing")
if cut_hand == 6:
for i in dic:
print(i, dic[i])
del_number = int(input("请输出您要删除的物品对应的序号: "))
del dic[str(del_number)]
if __name__ == '__main__':
work4()
案例3:用户管理系统
https://www.jianshu.com/writer#/notebooks/24632467/notes/27364818/preview