首页 > 代码库 > 购物程序

购物程序

shoplist = []
product_list = [
    (‘computer‘,5000),
    (‘apple‘,50),
    (‘book‘,42),
    (‘iphone‘,6800)
]
salary = input("输入您的工资:")
if salary.isdigit():
    salary = int(salary)
while True:
    for index,iteam in enumerate(product_list):
        print(index,iteam)
    user_choice = input("你想买什么?")
    if  user_choice.isdigit():
        user_choice = int(user_choice)
        if user_choice < len(product_list) and user_choice >=0:
            p_iteam = product_list[user_choice]
            if p_iteam[1] <= salary:
                shoplist.append(p_iteam)
                salary = salary - p_iteam[1]
                print("你购买了%s,您的余额为;%s" %(p_iteam,salary))
            else:
                print("你都没钱了,买不起啊!")
        else:
            print("没有您需要的产品!")
    elif user_choice == ‘q‘:
        print("----------购物清单----------")
        for p in shoplist:
            print(p)
        print("你的余额:", salary)
        exit()
    else:
        print("错误的类型")

 

购物程序