b编写的一个小程序,中间对于列表中的分隔符(,),没有输入使得程序无法运行,后来还有(%s)的使用不熟练,也是错误点。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
shoping_list=[]
product = [
("iphone",5800),
(‘mac pro‘,9800),
(‘bike‘,800),
(‘watch‘,8500),
(‘coffe‘,31),
]
salary = input("你的收入:")
if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(product):
print(index,item)
user_choice = input("选择商品:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice <len(product) and user_choice >= 0:
p_item = product[user_choice]
if p_item[1] <= salary:
shoping_list.append(p_item)
salary = salary - p_item[1]
print("添加 %s到你的购物车上,你现在的余额是%s"%(p_item[0], salary))
else:
print(‘\033[41:1m你的余额只剩[%s]啦,购买不了、033[om‘%salary)
else:
print("产品编码[%s]不存在"%user_choice)
elif user_choice == ‘q‘:
print(‘----------购物清单------‘)
for p in shoping_list :
print(p)
print(‘你现在的余额是:‘,salary)
exit()
else:
print(‘不可选择‘)