第一个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()

转载于:https://www.cnblogs.com/ZJU-missile/p/10405399.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值