欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

day2_作业1(购物车)

程序员文章站 2022-04-13 22:51:53
#!/usr/local/bin/python3 # -*- coding:utf-8 -*- balance=input("\033[36;1mPlease input your salary:\033[0m") Merchandise_list=[[1,'Mac pro',16888],[2,'... ......
#!/usr/local/bin/python3
# -*- coding:utf-8 -*-

balance=input("\033[36;1mPlease input your salary:\033[0m")
Merchandise_list=[[1,'Mac pro',16888],[2,'Iphone x',7888],[3,'Ps4 pro',4000]]
shopping_list=[]
if balance.isdigit():
    balance = int(balance)
    while True:
        print('\033[31;1mMerchandise list:\033[0m')
        for x in Merchandise_list:
            print(x)
        num=input("which one do you want?-->")
        if num.isdigit():   #判断输入类型是否是数字类型
            if int(num)<=len(Merchandise_list) and int(num)>0:
                if balance>=Merchandise_list[int(num)-1][2]:
                    balance-=Merchandise_list[int(num)-1][2]
                    shopping_list.append(Merchandise_list[int(num)-1])
                    print("your balance:\033[31;1m%s\033[0m"%(balance)) #设置字体颜色为红色,绿色为32
                else:
                    print("your balance:\033[31;1m%s\033[0m"%(balance))
                    print("\033[31;1myour balance was inadequate!\033[0m")
                    continue
            else:
                print("\033[41;1mthis product was not exist!\033[0m")
                print("your balance:\033[31;1m%s\033[0m" % (balance))
        elif num=='q':
            print("\033[43;1m----------your shopping list:----------\033[0m")
            for x in shopping_list:
                print("\033[35;1m%s\033[0m"%(x))
            print("your balance:\033[31;1m%s\033[0m" % (balance))
            exit()
        else:
            print("\033[41;1minvalid input!\033[0m")
            print("your balance:\033[31;1m%s\033[0m" % (balance))
else:
    print("\033[41;1minvalid input!\033[0m")
    exit()