首页 > 代码库 > python Day05

python Day05

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author Jean

shopping_list = []

product_list = [
("苹果",5800),
("华为",1800),
("魅族",2200),
("oppo",1900),
("vivo",1500),
("三星",800)
]

salary = input("Please Input Your salary: ")

if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(product_list):
print(index,item)
user_choice = input("请输入商品编号: ")
if user_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:
shopping_list.append(p_item)
salary -= p_item[1]
print("added %s into shopping cart. Your current balance \033[31;1m%s\033[0m" % (p_item, salary))
else:
print("你的余额为 %s 无法购买商品。" % (salary))

else:
print("无效的商品编号...")
elif user_choice == "q":
print("----以下为您选购的商品----")
for p in shopping_list:
print(p)
print("你的余额为 %s" % (salary))
exit(1)
else:
print("您输入的商品不存在!")
exit(1)

else:
print("Invliad Input....")

python Day05