#!/usr/bin/evn python
# -*- coding:utf-8 -*-
# Author: Aron
import sys
commodity = '''
1.苹果 200
2.橘子 350
3.香蕉 500
4.水蜜桃 750
5.罗明 2000
'''
ShoppingCart = []
wages = int(input("你的工资:"))
while True:
print(commodity)
s1 = input("想购买的商品序号:")
if s1 == '1':
ShoppingCart.append(['苹果','200'])
continue
elif s1 == '2':
ShoppingCart.append(['橘子','350'])
continue
elif s1 == '3':
ShoppingCart.append(['香蕉','500'])
continue
elif s1 == '4':
ShoppingCart.append(['水蜜桃','750'])
continue
elif s1 == '5':
ShoppingCart.append(['罗明','2000'])
continue
elif s1 == 'Q':
commodity1 = []
for i in list(ShoppingCart):
for i1 in list(i[::2]):
commodity1.append(i1)
for n in list(ShoppingCart):
for n1 in list(n[1:2]):
n1 = int(n1)
wages = wages - n1
for n in list(ShoppingCart):
for n1 in list(n[1:2]):
n1 = int(n1)
wages = wages - n1
if wages < 0:
wages = input("你的余额已经不足,请重新选购。请重新输入工资:")
ShoppingCart = [ ]
continue
else:
print("你够买的商品为:",list(commodity1))
print("你的余额为:%s" % wages)
sys.exit()
else:
print("输入错误,请重新输入:")
continue



学习完成列表以后:
#!/usr/bin/evn python
# -*- coding:utf-8 -*-
# Author: Roc wings

product_list = [
('Iphone',5800),
('Mac Pro',9800),
('Bike',800),
('Watch',10600),
('Cpffee',31),
('Alex Python',100)
]
shopping_list = []
salary = input("Input your salary:")
if salary.isdigit(): #如果是一个整数
salary = int(salary)
while True:
for index,item in enumerate(product_list): #for item in product_list:
print(index,item)#print(product_list.index(item),item)
user_choice = input("请选择购买商品>>>:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >= 0:
p_time = product_list[user_choice]
if p_time[1] <= salary:#买得起
shopping_list.append(p_time)
salary -= p_time[1]
print("Added %s into shopping,your current balance is \033[31;1m%s\033[0m" %(p_time,salary))
else:
print("\033[41;1m你的余额只剩[%s]啦,还买什么呢?\033[310m")
else:
print("商品[%s]不存在"% user_choice)
elif user_choice == 'q':
print("--------shopping list --------------")
for p in shopping_list:
print(p)
print("your current balance",salary)
exit()
else:
print("输入错误>>>")




posted on 2017-09-28 14:58  大千世界。  阅读(160)  评论(0)    收藏  举报