第一个python程序:Shopping List

Posted zju-missile

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第一个python程序:Shopping List相关的知识,希望对你有一定的参考价值。

# 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的主要内容,如果未能解决你的问题,请参考以下文章

Shopping cart program--python

python第二天

跟着Alex老师学习抄了一遍shopping_list的购物程序

作业购物车

从零单排Python学习第二课

python----作业0329