正在学习老男孩教育Alex老师的python视频课程,感谢。
版本1:自己瞎弄版
购物车:
#__author__ = 'Alex Wen'
# coding=gbk
Shopping={
"1101":{"Mac pro":int("8880")},
"1102":{"IPhone":int("5800")},
"1103":{"Watch":int("500")},
"1104":{"HuaWei P20":int("3780")},
"1105":{"Pad":int("1200")}
}
Bought_List=[]#设置一个空列表用于存放已经购买的商品数据
print("商品列表-->",Shopping)
Blance=input("你的余额为>>>:")
if Blance.isdigit():
Blance=int(Blance)
while True:
Buy_1=input("请输入要购买的商品编号>>>:")
if Buy_1 in Shopping.keys():#输入商品编号
Price_All=Shopping[Buy_1].values()#每一件商品的价格
for Name_Goods in Shopping[Buy_1].keys():#把编号对应的商品名取出
pass
for Price in Price_All:#把输入编号对应的商品价格取出
if Price<=Blance:#买得起
Bought_List.append(Name_Goods)
Blance-=Price
print("把%s添加到购物车,你的余额为%s"%(Name_Goods,Blance))
else:
print("支付失败!你的余额为%s"%(Blance))
elif Buy_1 == "q":
print("----------商品清单-----------")
for Totall_Goods in Bought_List:
print(Totall_Goods)
print("您目前的余额为%s"%(Blance))
else:
print("请输入正确的商品编号!谢谢!")
=========================================手动分割线==========================================
老男孩教育Alex老师版(基于列表):
#__author__ = "Alex Li"
product_list = [
('Iphone',5800),
('Mac Pro',9800),
('Bike',800),
('Watch',10600),
('Coffee',31),
('Alex Python',120),
]
shopping_list = []
salary = input("Input your salary:")
if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(product_list):
#print(product_list.index(item),item)
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:
p_item = product_list[user_choice]
if p_item[1] <= salary: #买的起
shopping_list.append(p_item)
salary -= p_item[1]
print("Added %s into shopping cart,your current balance is 33[31;1m%s 33[0m" %(p_item,salary) )
else:
print(" 33[41;1m你的余额只剩[%s]啦,还买个毛线 33[0m" % salary)
else:
print("product code [%s] is not exist!"% user_choice)
elif user_choice == 'q':
print("--------shopping list------")
for p in shopping_list:
print(p)
print("Your current balance:",salary)
exit()
else:
print("invalid option")