小白成长日记 day 2 破程序 (还是自己菜)4.26 10;28’

需求:


1启动程序后,让用户输入工资,然后读音商品列表


2允许用户根据商品编号购买商品


3用户选择商品后,检测余额是否足够,够就扣除,不够就提醒


4可以随时退出,退出时,打印购买商品和余额

shopping_list=[]
list=[
    ('Iphone',5800),
    ('Mac Pro',9800),
    ('Bike',800),
    ('Watch',10600),
    ('Coffee',31),
    ('Alex Python',120)
]
salary=input('please enter your salary;')
if salary.isdigit():#大体意思是输入的是数字
    salary=int(salary )#转成数字
    while True:
        for index,i in enumerate (list):#index是下标  从列表第一个开始循环
            print(index ,i)
        choose=input('你想要买什么==>:')
        if choose .isdigit():
            choose =int(choose)
            if choose <len(list) and choose>=0:
                a=list[choose]#  a表示要购买的那个商品的位置
                if a[1]<=salary:   #a【1】表示要购买的商品的价格 a表示商品`1
                    salary=salary-a[1]
                    shopping_list.append(a)
                    print('add %s your shopping_list,并且剩下%s工资'  %(a,salary))
                else:
                    print('你还剩下%s工资 买个屁 啊!!!'%salary )
            else:
                print('product code [%s] is not exist!'%choose )
        elif choose =='q':
            print('--------shopping list------')
            for p in shopping_list:
                print(p)
            print('add %s your shopping_list,并且剩下%s工资'  %(a,salary))
            exit()
        else:
            print('错误选项')

else:
    print('请输入数字:')



老话;背单词 句式

猜你喜欢

转载自blog.csdn.net/weixin_42121899/article/details/80173028
今日推荐