需求:
啟動程式後,讓使用者輸入工資,然後列印商品列表
允許使用者根據商品編號購買商品
使用者選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒
可隨時退出,退出時,列印已購買商品和餘額
product_list =[("iphone
",5800),
("mac pro
",9800),
("bike
",800),
("watch
",10600),
("coffee
",31),
("book
",120),
]shopping_list =
salary = input("
input your salary:")
ifsalary.isdigit():
salary =int(salary)
while
true:
'''for item in product_list:
print(product_list.index(item),item)
break
'''#
方法一,其中break為test時跳出迴圈
for index,item in
enumerate(product_list):
(index,item)
user_choice = input("
選擇要買嗎?>>>>")
ifuser_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: #
買得起 salary -= p_item[1]
print("
add %s into shopping cart, your current balance is \033[31;1m%s\033[0m
"%(p_item,salary))
else
:
print("
\033[41;1m你的餘額只剩[%s]啦,還買個毛線\033[0m
"%salary)
else
:
print("
product code [%s] is not exist!
" %user_choice)
elif user_choice == 'q'
:
print("
-------shopping list--------")
for p in
shopping_list:
(p)
print("
your current balance:
",salary)
exit()
else
:
print("
invalid option
")
購物車程式
要求 1 啟動程式,讓使用者輸入工資,列印商品列表2 允許使用者根據商品序號購買商品3 使用者選擇商品後,檢測餘額是否足夠,夠則直接扣款,不夠提示餘額不足4 可隨時退出,退出時列印商品列表 product list mac 180 迪奧 300 阿瑪尼 490 shopping list salar...
購物車程式
作業需求 資料結構 goods 功能要求 基礎要求 1 啟動程式後,輸入使用者名稱密碼後,讓使用者輸入工資,然後列印商品列表 2 允許使用者根據商品編號購買商品 3 使用者選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒 4 可隨時退出,退出時,列印已購買商品和餘額 5 在使用者使用過程中,關鍵...
購物車程式
作業需求 資料結構 goods 功能要求 基礎要求 1 啟動程式後,輸入使用者名稱密碼後,讓使用者輸入工資,然後列印商品列表 2 允許使用者根據商品編號購買商品 3 使用者選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒 4 可隨時退出,退出時,列印已購買商品和餘額 5 在使用者使用過程中,關鍵...