2018-07-06 21:06:16 ...
功能要求:
要求用戶輸入自己擁有總資產,例如:2000
顯示商品列表,讓用戶根據序號選擇商品,加入購物車
購買,如果商品總額大於總資產,提示賬戶餘額不足,否則,購買成功。
goods = [
{"name": "電腦", "price": 1999},
{"name": "滑鼠", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
goods = [ {"name": "電腦", "price": 1999}, {"name": "滑鼠", "price": 10}, {"name": "游艇", "price": 20}, {"name": "美女", "price": 998}, ] a = 0 count = 1 dic = {} li = [] c = 0 sum = 0 s = int(input("請輸入你的資產:")) while count < 5: print("%s %s %s" % (count,goods[a]["name"],goods[a]["price"])) dic[count] = goods[a] count += 1 a += 1 while 1: s1 = input("請你選擇要加入購物車的商品(按Q結束選購進行付款):") if s1.upper() == "Q": break elif int(s1) > int(4): print("輸入有誤,請重新輸入") else: li.append(dic[int(s1)]["price"]) print("%s" % (dic[int(s1)]["price"])) c += 1 if int(li[0]) + int(li[c - 1]) > int(s): print("戶餘額不足") else: print("購買成功!")
2018-07-06 21:06:16
“繼續奔跑 輸掉一切也不要輸掉微笑”