码迷,mamicode.com
首页 > 编程语言 > 详细

第一个python程序:Shopping List

时间:2019-02-20 11:32:19      阅读:168      评论:0      收藏:0      [点我收藏+]

标签: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()

第一个python程序:Shopping List

标签:pytho   item   cart   number   exit   iphone   sel   ice   demo   

原文地址:https://www.cnblogs.com/ZJU-missile/p/10405399.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!