标签:数据 余额 允许 user 解答 lan 颜色 索引 into
1、想快点学会,唯有不断地练习。练习中严格要求。
2、题目
#启动程序后,让用户输入工资,然后打印商品列表
#允许用户根据商品编号购买商品
#用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
#可随时退出,退出时,打印已购商品和余额
3、解答过程
commodity_list = [(‘Ipone‘,6800),
(‘Tea‘,1200),
(‘Pencil‘,12),
(‘Apple‘,85),
(‘Radio‘,120),
(‘Bike‘,1800),]
salary = input(‘input your salary:‘)
shopping_list = []
if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(commodity_list,start=1):
print(index,item)
user_choice = input(‘Please choice what you want ?>>>:‘)
if user_choice.isdigit():
user_choice =int(user_choice)
if user_choice >=0 and user_choice < len(commodity_list):
p_item = commodity_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 is\033[31;1m %s:\033 [0m‘%(p_item,salary))
else:
print("\033[41;1mYour current salary is %s,Your money is not enough!\033[0m")
else:
print(‘Invalid option!")
elif user_choice = "q":
print("--------------- shopping list ---------------)
for p in shopping_list:
print(p)
print("Your current balance:",salary)
exit()
经过测试调整:
#准备好商品列表
commodity_list = [(‘Ipone‘,6800),
(‘Tea‘,1200),
(‘Pencil‘,12),
(‘Apple‘,85),
(‘Radio‘,120),
(‘Bike‘,1800)]
#提示输入你准备的购物款
salary = input(‘input your salary:‘)
#准备空的购物车
shopping_list = []
#isdigit()判断输入的是否为十进制数字
if salary.isdigit():
salary = int(salary)
while True:
#enmuerate()将商品列表组合为索引系列,同时列出数据下标和数据。
for index,item in enumerate(commodity_list):
#提供购物列表供选择
print(index,item)
#提示选择购物
user_choice = input(‘Please choice what you want ?>>>:‘)
if user_choice.isdigit():
user_choice =int(user_choice)
#确保在提供的购物列表范围内选择
if user_choice >=0 and user_choice < len(commodity_list):
#将顾客从购物列表中选中的商品储存到变量p_item中
p_item = commodity_list[user_choice]
#选中商品价格与购物款作比较,判断是否买得起
if p_item[1] <= salary:
#买得起就放进购物车
shopping_list.append(p_item)
#从总购物款中扣除
salary -= p_item[1]
#\033[31;1m?\033[0m 为?选择颜色
print(‘Added %s into shopping cart,your current balance is\033[31;1m%s:\033[0m‘%(p_item,salary))
else:
print("\033[41;1mYour current salary is %s,Your money is not enough!\033[0m"%salary)
else:
#输入超出购物列表范围
print(‘Invalid option!‘)
elif user_choice == "q":
print("--------------- shopping list ---------------")
for o,p in enumerate(shopping_list):
print(o,p)
print("Your current balance:",salary)
exit()
标签:数据 余额 允许 user 解答 lan 颜色 索引 into
原文地址:https://www.cnblogs.com/walch2006/p/12244803.html