老男孩Python 3.5学习第02周作业——购物车

Readme:

本程序要求用户先输入工资金额,然后在商品列表中选择商品,程序会自动扣减余额,最后按照用户要求打印购物清单。

流程图:

1.建立商品列表与初始为空的购物清单。

2.在要求用户输入工资金额之后,利用while true循环实现重复选择界面。

3.先判定用户输入编号是否合法,然后判断工资金额是否足够支付,再打印商品、显示余额,最后输出购物清单。

product_list = [
('iPhone',5200),
('iMac pro',12000),
('iPad',3888),
('Watch',2888)
]
shopping_list = []
salary = input('您的工资是:')
if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(product_list):
print(index+1,item)
your_choice = input('您的选择是:')
if your_choice.isdigit():
your_choice = int(your_choice)
if your_choice <= len(product_list) and your_choice > 0:
if salary > product_list[your_choice - 1][1]:
shopping_list.append(product_list[your_choice - 1])
print(shopping_list)
salary -= product_list[your_choice - 1][1]
print('您的余额为:%d'%(salary))
else:
print('您的余额不足')
else:
print('请在商品列表中选择。')
elif your_choice == 'q':
print(shopping_list)
exit()
else:
print('请输入想要购买的商品编号。')

else:
print('请输入数字字符。')

猜你喜欢

转载自www.cnblogs.com/lsj000jsl/p/10171735.html