购物车程序练习--Python(Alex视频学习)20180609

程序练习:购物车程序

要求:

1.启动程序后,让用户输入工资,然后打印商品列表

2.允许用户根据商品编号购买商品

3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒

4.可随时退出,退出时,打印已购买商品和余额

#商品清单
product_list=[
("IPhone", 5800),
("Mac Pro",12000),
("Starbuck Latte",31),
("Alex Puthn",81),
("Bike",800)
]
salary=input("Input your salary:")
shopping_list=[]
if salary.isdigit():#判断输入工资是否为整数 ,并转化为整数
salary=int(salary)
while True:
for item in product_list:
print(product_list.index(item),item)
buy_choice=input("您需要购买的商品编号是:")
if buy_choice.isdigit():
buy_choice=int(buy_choice)
if buy_choice<len(product_list) and buy_choice>=0:#判断商品编码是否在所列清单里
p_item=product_list[buy_choice]
if p_item[1]<=salary:#判断是否能够买得起
shopping_list.append(p_item)
salary-=p_item[1]
print(shopping_list)
print("your current balance is \033[31;1m%s\033[0m,thank you!" % salary)
else:
print("\033[41;1myour money is %s, not enough to bus goods\033[1m" % salary)
else:
print("product is not exist")
elif buy_choice=="q":#退出程序控制
print("----shoppinp list-----")
for p in shopping_list:
print(p)
print("your banlance is:", salary)
exit()
else:
print("Input is wrong")

猜你喜欢

转载自www.cnblogs.com/pei-jennifer/p/9159400.html