首页 > 编程语言 > 详细

python程序2:简易购物车

时间:2021-01-15 09:56:47      阅读:24      评论:0      收藏:0      [点我收藏+]

`#!/usr/bin/env python3

* encoding:utf8_*_

author:wang

product = [
(‘oppo‘,3000),
(‘hasee‘,8000),
(‘bike‘,800),
(‘python book‘,80),
(‘coffee‘,20)
]
salary=input("pls input your salary:")
shopping_list = [] #购物车
if salary.isdigit(): #判断输入的是否为整数
salary=int(salary) #强行转换为整数
while True: #死循环
for index,item in enumerate(product): #函数enumerate会自动生成index和item
print(index,item)
user_choice=input("select buy>>>>")
if user_choice.isdigit():
user_choice=int(user_choice)
if user_choice < len(product) and user_choice>=0:
p_item=product[user_choice]
if p_item[1] <= salary:
shopping_list.append(p_item)
salary -= p_item[1]
print("your buy product \033[31:1m %s \033[0m,only \033[31:1m %s \033[0m rmb" %(p_item,salary))
else:
print("your salary only \033[31:1m %s \033[0m rmb,don‘t buy this prod\033[31:1m %s \033[0m" %(p_item,salary))
else:
print("the prod isn‘t exist.")
elif user_choice == "q":
print("you buy prod:".center(50,‘-‘))
for p in shopping_list:
print(p)
print("you only salary\033[32;1m %s \033[0m rmb" %(salary))
exit()
else:
print("input error.")`

python程序2:简易购物车

原文:https://www.cnblogs.com/wang50902/p/14280144.html

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