首页 > 编程语言 > 详细

python学习day4--python基础--购物小程序

时间:2016-09-01 18:42:24      阅读:185      评论:0      收藏:0      [点我收藏+]
‘‘‘
购物小程序:
用户启动时先输入工资
用户启动程序后打印商品列表
允许用户选择购买商品
允许用户不断购买各种商品
购买时检测余额是否够,如果够直接扣款,否则打印余额不足
允许用户主动退出程序,退出时打印已购商品列表
‘‘‘
salary=input("input you salary:")
if salary.isdigit():
salary=int(salary)
else:
exit("invalid data type.....")
welcome_msg=‘welcome to Shopping mall‘.center(50,‘-‘)
print(welcome_msg)
product_list=[
(‘Iphone‘,5888),
(‘Mac Air‘,8000),
(‘Mac Pro‘,9000),
(‘XiaoMi‘,19.9),
(‘Coffee‘,30),
(‘Tesla‘,820000),
(‘Bike‘,900),
(‘cloth‘,200)
]
shop_car=[]
exit_flag=False
while not exit_flag:

print(‘product list‘.center(50, ‘-‘))
#for product_item in product_list:
#p_name, p_price = product_item
‘‘‘for p_name,p_price in product_list:
print(p_name,p_price)‘‘‘
for item in enumerate(product_list):
index=item[0]
p_name=item[1][0]
p_price=item[1][1]
print(index,p_name,p_price)
user_choice=input("[q=quit,c=check]What do you want to buy?:")
if user_choice.isdigit():#肯定是选择商品
user_choice=int(user_choice)
if user_choice<len(product_list):
p_item=product_list[user_choice]
if p_item[1]<salary:
shop_car.append(p_item)#放入购物车
salary -=p_item[1]#减钱
print("Added [%s] into shop car,your current balance is [%s]"%(
p_item,salary))
else:
print("your balance is [%s],can not afford this.."%salary)
else:
if user_choice==‘q‘ or user_choice=="quit":
print("purchased products as belows:".center(40,‘*‘))
for item in shop_car:
print(item)
print("End".center(40,‘*‘))
print("your balance is [%s]"%salary)
exit_flag=True
elif user_choice==‘c‘ or user_choice=="check":
print("purchased products as belows:".center(40,‘*‘))
for item in shop_car:
print(item)
print("End".center(40,‘*‘))
print("your balance is [%s]"%salary)

python学习day4--python基础--购物小程序

原文:http://www.cnblogs.com/gpdm/p/5830682.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!