1. 程式人生 > >Shopping cart program--python

Shopping cart program--python

clas The wro for pri pen pin odin digi

購物車小程序

需求描述如下:

1.提示用戶輸入薪水

2.用戶輸入薪水後,打印商品編號、內容及價格

3.提醒用戶輸入商品代碼,若余額大於等於商品價格,可購買;若小於,提示用戶余額不足

4.用戶輸入q ,退出交易,打印用戶購買的商品和余額

技術分享圖片
#-*-coding:utf-8 -*-

goods_list=[(Iphone,5800),(Mac Pro,12000),(Coffee,31),(Book,81),(Bike,8000)]
goods_bought=[]
salary=input("please input your salary:")
if salary.isdigit():
    salary
=int(salary) balance=salary while True: print("This is our goods list:") for index,item in (enumerate(goods_list)): print(index,item) goods_number=(input("please input goods number you want:")) if goods_number.isdigit(): goods_number=int(goods_number)
if goods_number>=0 and goods_number<(len(goods_list)): if goods_list[goods_number][1]<=balance: goods_bought.append(goods_list[goods_number]) balance=balance-goods_list[goods_number][1]%(goods_list[goods_number].balance)
else: print("Your balance %s is not enough!"%balance) else: print("The goods number %s is not exist!"%goods_number) elif goods_number==q: print("Your goods are:") for item in goods_bought: print(item) print("your balance is:%s"%balance) goods_bought.clear() exit() else: print("Your input is wrong!") else: print("your salary is wrong:")
View Code

Shopping cart program--python