当前位置:首页 > Python教程 > python技巧

Python 简单购物程序

# Author:Eric Zhao
# -*- coding:utf-8 -*-
‘‘‘需求:
启动程序后,让用户输入工资,然后打印商品列表
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额‘‘‘
product_list = [
(‘IPhone‘,5000),
(‘Bike‘, 500),
(‘Car‘, 50000),
(‘Hat‘, 50)
]
shopping_list = []
salary = input(‘Please input your salary..‘)
if salary.isdigit():
salary = int(salary)
while True: # 死循环
# for item in product_list:
# print(product_list.index(item),item)
for index,item in enumerate(product_list):
print(index,item)
user_choice = input(‘Please type a product number,if type q then exit..‘)
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice>=0:
choice_list = product_list[user_choice]
if salary >= choice_list[1]: # 买得起
shopping_list.append(choice_list)
salary = salary - choice_list[1]
print(‘Added %s into shopping cart,your balance is33[31;1m%d33[0m‘%(choice_list,salary))
else:
print(‘Your balance is only 33[31;1m%d33[0m,not enough..‘%salary)
else:
print(‘The product number [33[31;1m%d33[0m] doesn‘t exist..‘%user_choice)
elif user_choice == ‘q‘:
print(‘------------------- shopping list --------------------‘)
for mylist in shopping_list:
print(mylist)
print(‘Your balance is 33[31;1m%d33[0m‘%salary)
exit()
else:
print(‘Invalid number‘)
else:
print(‘Please type a integer..‘)




原文:http://www.cnblogs.com/abarcher/p/7801544.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐