乙個python購物車迴圈程式:
輸入工資》列印列表選擇商品》確認購買?(不確認返回商品列表)》計算餘額、已購商品計件、累計消費》繼續列印商品列表
》確認購買?(q選擇退出》退出之後列印已購商品、餘額)
# coding=utf-8
# version:python3.6
# name:shiwei
# 購物車程式
product_list = [
['phone', '600'],
['computer', '500'],
['camera', '7000'],
['mac', '900'],
] # 產品列表
spend = 0 # 初始累計花費為0
total = 5000 # 今天花的錢不超過這些數
count = 0 # 計件
while true:
salary = input("iuput your salary:")
if salary.isdigit():
salary = int(salary)
while true:
# for item in product_list:從商品列表裡面迴圈列印
# print(product_list.index(item),item)
for index, item in enumerate(product_list): # enumerate 把列表變成乙個類似矩陣的形式,有序號和內容
print('\t',index, item)
choice = input(">>>\t6choose one commodity you like:")
if choice.isdigit(): # 判斷輸入的是不是商品
choice = int(choice)
if choice>-1 and choicetotal:
print('you spent too much money!')
continue
else:
print('see if there is anything else you like?')
continue # \033[031;1m%s\033[0m 給這個%s加紅色
else:
print('\033[042;1myour money is not enough!\033[0m')
else:
print('product code is not exist!')
elif choice == 'q':
print("----you』ve bought \033[035;1m%s\033[0m products----"%(count) )
for item_s in shopping_list: # 列印購物車裡面的商品
print('>>>', item_s[0])
# for item_s in shopping_list:把購買商品的錢加起來
# spend+=item_s[1]
print(" cost you \033[033;1m%s\033[0m yuan, your balance is \033[031;1m%s\033[0m" %(spend, salary))
print('welcome to visit next time! good luck!')
#break
exit() # 結束所有迴圈了, 退出
else:
print('invalid choice!')
else:
print('error iuput, please input a constant! ')
continue
python 購物車程式
程式 購物車程式 需求 啟動程式後,讓使用者輸入工資,然後列印商品列表 允許使用者根據商品編號購買商品 使用者選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒 可隨時退出,退出時,列印已購買商品和餘額 while true salary int input 請輸入您的工資 info 商品資訊 1...
python購物車程式
目的 1 啟動程式後,讓使用者輸入工資,然後列印商品列表 2 允許使用者根據商品編號購買商品 3 使用者選擇商品後,檢測餘額是否夠,夠直接扣款,不夠就提醒 4 可隨時退出,退出時列印已購買商品和餘額 product list tuppercup 90 huawei mobile 4390 cloth...
購物車程式 python
啟動程式後讓使用者輸入工資,列印商品列表,允許使用者根據商品編號購買商品,使用者選擇商品後根據餘額扣款,餘額不夠則提醒,退出時,列印已購買商品及餘額 import sysprint product t shirt dress hat price 80 98 33 user lst 使用者已經購買的商...