天天看點

python購物車代碼_Python代碼之購物車

python購物車代碼_Python代碼之購物車

# -*- coding:utf-8 -*-

#Author:Mr Gan

不足之處:輸入商品編号時必須是整型,其他類型會報錯

import sys

balance = int(input("your money:"))

shopping_bag = [] #建立購物包

shopping_list = [["iphone",5800],["watch",2600],["book",56],["computer",7800]]

while True:

for index,i in enumerate (shopping_list):

print(index,i)

pro_num = input("production number:")

with open("餘額檔案",'w+') as left_money:

if pro_num == "q":

left_money.write(balance)

print("your balance left \033[31;1m%s\033[0m" % balance)

sys.exit()

elif pro_num.isdigit(): #判斷輸入的商品編号是否是數字

pro_num = int(pro_num)

#pro_name = shopping_list[pro_num]

if 0<= pro_num

pro_name = shopping_list[pro_num]

if pro_name[1] <= balance:

shopping_bag.append(shopping_list[pro_num][0])

balance -= pro_name[1]

else:

print("your banlance is not ennough,please choose others")

else:

print("no production,choose again")

for i in shopping_bag: #周遊已購買的商品

print("\033[31;1m%s\033[0m" % i)#列印所有已經購買的商品

print("your balance left \033[31;1m%s\033[0m" % balance)

pass