#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# name:zzyu
shop_list = [["手机",5000],
["电脑",7000],
["香蕉",50],
["衣服",200],
["车",10000]]
shop_list_2 = []
salary = input("请输入您的工资:")
if salary.isdigit():
salary = int(salary)
info = ‘‘‘<<<您的工资是:\033[31;1m{_salary}\033[0m,请选择您需要的商品编号>>>‘‘‘.format(_salary=salary)
print(info)
while True:
for index,itme in enumerate(shop_list):
print (index,itme)
user_choice = input("请选择您要购买的商品编号:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(shop_list) and user_choice >= 0:
shop_list_2.append(shop_list[user_choice])
user_choice_2 = shop_list[user_choice]
if user_choice_2[1] <= salary:
print("已为您添加到购物车:", shop_list_2)
salary -= shop_list[user_choice][1]
print("您的余额为:\033[31;1m{}\033[0m".format(salary))
else:
print ("您的剩余余额为:\033[31;1m{_salary}\033[0m,请及时充值 !!!".format(_salary=salary))
break
else:
print("没有此商品。。。")
elif user_choice == "q":
print("您已加入购物车的商品有:")
for p in shop_list_2:
print(p)
exit()
else:
print("您输入有误,请重新输入。。。。。")
else:
print("<<<<<<您输入有误,请重新输入>>>>>>>")
原文:https://www.cnblogs.com/zzyuhehe/p/12715791.html