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('\033[31;1mCONTENT\033[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('你的余额为:\033[31;1m{}\033[0m'.format(salary))
            break    
        else:
            print('输入错误啦')
else:
    print('工资格式不对!')
    exit(-1)

  

猜你喜欢

转载自www.cnblogs.com/lcxiao/p/11367110.html
今日推荐