每天进步一点点-------python之超市库存

一个礼拜又结束了,外面持续大雾霾天,我也懒得出去就躲在自习室里啃面包。不知道是不是越到后面越难的关系,这几课我都学得有点云里雾里,看着正确的代码就会“噢,原来如此”,但是自己是怎么都想不出来。。。。。真苦闷


今天这个是计算购物单的价格以及确定超市库存的小代码,看得我好迷糊


shopping_list = ["banana", "orange", "apple"]

stock = { "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = { "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

def compute_bill(shopping_list):
    total = 0
    for x in shopping_list:
        if stock[x] != 0:             #有库存的才能计算价格
            total += prices[x]
            stock[x]-= 1              #卖出去一个库存就少一个
    return total
    
print compute_bill(shopping_list)
print stock

课程提示说这个函数只能用来计算购物单的总价,我也试过如果后面跟单个物品会报错



你可能感兴趣的:(python)