阅读背景:

Python基础练习之购物车

来源:互联网 
#前置知识点
# enumerate(LIST)
#  输出一个元组,第一个为下标,第二个为元素

# a = [1, 2, 3, 4]
# for i in enumerate(a):
#     print(i)
"""
(0, 1)
(1, 2)
(2, 3)
(3, 4)
"""
# for i, item in enumerate(a):
#     print(i, item)
"""
0 1
1 2
2 3
3 4
"""
#len(LIST)  #返回列表长度

# print("3[31;1mCONTENT3[0m")   

#输出为红色的CONTENT

#exit()  #退出办法


#=====================
#Begin
#
productList = [
    ("iphone", 5000),
    ("watch", 20000),
    ("coffee", 1000),
    ("pencil", 500),
    ("switch", 2000),
    ("Audi", 200000),
]
purchasedList = []

salary = input("请输入你的工资金额:")
if salary.isdigit():
    salary = int(salary)
    while True:
        for i, item in enumerate(productList):
            print(i, item)

        userChoice = input("要购置东西吗?")
        if userChoice.isdigit():
            userChoice = int(userChoice)
            if userChoice < len(productList) and userChoice >= 0:
                userWantBuy = productList[userChoice]
                if userWantBuy[1] <= salary:
                    purchasedList.append(userWantBuy)
                    print("已添加",userWantBuy[0],"到购物车")
                    salary -= userWantBuy[1]
                else:
                    print("你的钱不够哇!")
            else:
                print("你选择的:",userChoice,"商品不存在!")
        elif userChoice.lower() == "q":
            print("=======已购置的商品=======")
            for i in purchasedList:
                print(i)
            print("你的余额为:3[31;1m{}3[0m".format(salary))
            break    
        else:
            print("输入毛病啦")
else:
    print("工资历局不对!")
    exit(-1)
#前置知识点
# enumerate(LIST)
#  输出一个元组,第一个为下标,第二个为元



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: