python之购物车version 2.0

简介:
#author:zhouyu
produce_list = [
    ('Watch'
,1000),
    ('Telephone',6500),
    ('Television',4500),
    ('refrigerate',2300),
    ('washer',680),
    ('compute',5500)
]
#
定义一个空的列表,表示购物车
shopping_list = []
#让用户输入工资
salary = input("Please input your salary: ")
#isdigit这个函数是用来判断这个salary是不是数字字符串,如果是就为True
if salary.isdigit():
#把工资转化为整数型
    salary = int(salary)
    while True:
#列出商品,enumerate这个函数可以列出这个列表的索引,按照下面方法来实验。
        for index,i in enumerate(produce_list):
           
print(index,i)
#让用户输入自己想要的商品
        user_choose = input("Please input the number of what do you want: ")
        if
user_choose.isdigit():
           
user_choose = int(user_choose)
#len函数可以列出列表的个数。
            if user_choose >= 0 and user_choose < len(produce_list):
#定义一个函数,这个函数等于用户选择的商品
                p_item = produce_list[user_choose]
#判断用户选择的商品的价格是否大于工资
                if p_item[1] <= salary:
#用户选择的商品小于工资则把商品加入到shopping_list上
                    shopping_list.append(p_item)
#用户购买这个商品后,工资就会自动减少
                    salary -= p_item[1]
                   
print("Add %s into shopping cart,and your balance is \033[31;1m%d\033[0m" %(p_item[0],salary))
#用户选择的商品大于工资则输出下面信息
                else:
                   
print("\033[41;1myour balance is not enough,get out there!!!\033[0m")
#如果用户输入的数字还是大于0小于len(produce_list)的话就输出以下信息
            else:
               
print("\033[41;1mthe produce is not exists!!!\033[0m")
#如果用户输入q就表示quit,就打印出用户购买的商品和余额并退出
        elif user_choose == 'q':
           
print("----------product list -----------")
            for
i in shopping_list:
               
print(i)
           
print("your balance is ", salary)
           
exit(1)
#如果用户输入的不是数字字符串就输出以下信息
        else:
           
print("Invali number !!!")

 


本文转自 周子琪 51CTO博客,原文链接:http://blog.51cto.com/izhouyu/1964012


相关文章
|
计算机视觉 Python
python opencv:ERROR: Could not find a version that satisfies the requirement opencv ERROR:
python opencv:ERROR: Could not find a version that satisfies the requirement opencv ERROR:
515 2
|
TensorFlow 算法框架/工具 iOS开发
【Python-Tensorflow】ERROR: Could not find a version that satisfies the requirement tensorflow
本文讨论了在安装TensorFlow时遇到的版本兼容性问题,并提供了根据Python版本选择正确pip版本进行安装的解决方法。
2744 1
|
开发者 Python
【Python】已解决:(Python3中pip无法安装urllib报错问题) ERROR: Could not find a version that satisfies the requireme
【Python】已解决:(Python3中pip无法安装urllib报错问题) ERROR: Could not find a version that satisfies the requireme
1870 0
【Python】已解决:(Python3中pip无法安装urllib报错问题) ERROR: Could not find a version that satisfies the requireme
|
开发者 Python
【Python】已解决:(pandas read_excel 读取Excel报错)ImportError: Pandas requires version ‘2.0.1’ or newer of ‘x
【Python】已解决:(pandas read_excel 读取Excel报错)ImportError: Pandas requires version ‘2.0.1’ or newer of ‘x
1359 0
|
数据库 Python
【Python】已解决:Pandas requires version ‘1.4.0’ or newer of ‘sqlalchemy’ (version ‘0.7.10’ currently ins
【Python】已解决:Pandas requires version ‘1.4.0’ or newer of ‘sqlalchemy’ (version ‘0.7.10’ currently ins
1027 0
|
API 计算机视觉 开发者
【Python】已解决:(pip安装PIL库报错) ERROR: Could not find a version that satisfies the requirement PIL (from v
【Python】已解决:(pip安装PIL库报错) ERROR: Could not find a version that satisfies the requirement PIL (from v
3185 0
|
计算机视觉 索引 Python
【Python】已解决:ERROR: Could not find a version that satisfies the requirement cv2 (from versions: none)
【Python】已解决:ERROR: Could not find a version that satisfies the requirement cv2 (from versions: none)
1887 0
|
开发者 Python
【Python】已解决:ERROR: Could not find a version that satisfies the requirement easyocr (from versions: n
【Python】已解决:ERROR: Could not find a version that satisfies the requirement easyocr (from versions: n
1036 0
|
开发者 Python
【Python】已完美解决:ERROR: Could not find a version that satisfies the requirement re
【Python】已完美解决:ERROR: Could not find a version that satisfies the requirement re
3581 0
|
Shell Linux 网络安全
【Python】已完美解决:(pip提示升级)WARNING: There was an error checking the latest version of pip.,
【Python】已完美解决:(pip提示升级)WARNING: There was an error checking the latest version of pip.,
7645 0

推荐镜像

更多