标签:pytho item cart number exit iphone sel ice demo
# This is my first program using python
# Shopping cart demo.
product_list = [
(‘iphone‘,5800),
(‘Mac Pro‘,9800),
(‘Bike‘,800),
(‘Watch‘,1800),
(‘Coffee‘,31),
(‘Book‘,20),
]
shopping_list=[] # 空列表-购物清单
salary = input("Please input your total money:") # 输入购物金额
while 1-salary.isdigit():
salary = input(‘Please input your total money in digital!:‘)
else:
salary = int(salary) # 将购物金额转成整数数值
while True:
print(‘--------product list-----------‘) # 每次购物输入购物清单
for item in product_list:
print(product_list.index(item), item)
choice = input(‘Please select the item number.‘)
choice = int(choice)
if choice<len(product_list) and choice>=0:
item = product_list[choice]
else:
print(‘\033[31;1mYour input is wrong !\033[0m‘)
continue
# 判断金额是否满足购物要求
if salary>=item[1]:
salary -= item[1]
print("Add %s into the shopping cart, your balance is %s" % (item[0], salary))
shopping_list.append(item)
elif salary<item[1] and salary>=20:
print("\033[31;1mYour money is not enouch for this item.\033[0m")
else:
print("\033[31;1mPlease get more money for shopping. Thank you !\033[0m")
if shopping_list != []:
print(‘----------Shopping list----------‘)
for j in shopping_list:
print(shopping_list.index(j), j)
exit()
# 询问是否还需要购物及打印购物清单
continue_shopping = input(‘Do you want to continue shopping? (Y/N)‘)
if continue_shopping==‘N‘:
if shopping_list == []:
print("\033[31;1mYou haven‘t bought anything yet.\033[0m")
else:
print(‘----------Shopping list--------‘)
for j in shopping_list:
print(shopping_list.index(j), j)
print(‘\033[31;1mYour balance is %s.Thank your shopping.\033[0m‘ % (salary))
exit()
标签:pytho item cart number exit iphone sel ice demo
原文地址:https://www.cnblogs.com/ZJU-missile/p/10405399.html