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

Python 简单购物程序

时间:2017-11-07 23:55:26      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:输入   only   author   coding   end   打印   --   append   val   

# 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 is\033[31;1m%d\033[0m‘%(choice_list,salary))
else:
print(‘Your balance is only \033[31;1m%d\033[0m,not enough..‘%salary)
else:
print(‘The product number [\033[31;1m%d\033[0m] doesn\‘t exist..‘%user_choice)
elif user_choice == ‘q‘:
print(‘------------------- shopping list --------------------‘)
for mylist in shopping_list:
print(mylist)
print(‘Your balance is \033[31;1m%d\033[0m‘%salary)
exit()
else:
print(‘Invalid number‘)
else:
print(‘Please type a integer..‘)








Python 简单购物程序

标签:输入   only   author   coding   end   打印   --   append   val   

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

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