salary = input("please input the whole price:")
if salary.isdigit(): #判断输入的是否为数字
salary = int (salary)
product_list = [ #定义商品
["book",50],
["telephone",2000],
["clothes",899],
["glass",399]
]
shopping_list = [] #已买商品
while True:
for item in product_list: #打印所有商品
print(product_list.index(item)+1,item)
user_choice = input("please choose commodities you want to buy")
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 - 1] #选择对应序号的商品
if salary >= p_item[1]: #判断价钱是否足够
shopping_list.append(p_item) #将已经购买的商品添加到已买商品中
salary -= p_item[1] #价格对应减少
print("%s has added the shopping cart,your current salary is \033[31;1m%s\033[0m" %(p_item[0],salary))
else:
print("\033[31;1myour current salary is not enough to pay,it noly have %s\033[0m" %(salary))
else:
print("your choice is not in.")
elif user_choice == "q": #输出为q,打印已购商品内容以及所剩钱数
print("-------shopping list---------")
for i in shopping_list:
print(shopping_list.index(i)+1,i)
print("your current balance is",salary)
exit()
else:
print("please input correct number")
python之简单购物车练习
猜你喜欢
转载自blog.csdn.net/sundan614/article/details/106925903
今日推荐
周排行