Result None, instead of 7


#1

Not sure what the issue is here, it prints out the right amounts.

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

stock = {
    "banana"	: 6,
    "apple"		: 0,
    "orange"	: 32,
    "pear"		: 15
}

prices = {
    "banana"	: 4,
    "apple"		: 2,
    "orange"	: 1.5,
    "pear"		: 3
}

# Write your code below!
def compute_bill(food):
    total = 0
    for key in food:
        if stock[key] > 0:					# checks if there is stock
            total = total + prices[key]		# adds the price of fruit bought
            stock[key] - 1					# reduces the stock inventory
            print total	

#compute_bill(shopping_list) 				# expecting $5.50
#compute_bill(shopping_list2)				# expecting $7

Error code

Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in None instead of the correct 7

#2

the function needs to return the correct total, currently you are only printing total

by default, function return None, that is a big clue (maybe useful for later)


#3

Thank you !

Pertinent point! I had it in earlier but kept getting some strange error.
With return correctly added, it spits out the following:

Oops, try again. calling compute_bill with a list containing 1 orange and 7 bananas resulted in 29.5 instead of the correct 25.5

Basically tells me that my

if stock[key] > 0:

is not working somewhere, I assume that it is still summing everything, even if there is not sufficient stock of said item.

Here is the full updated code:

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

stock = {
    "banana"	: 6,
    "apple"		: 0,
    "orange"	: 32,
    "pear"		: 15
}

prices = {
    "banana"	: 4,
    "apple"		: 2,
    "orange"	: 1.5,
    "pear"		: 3
}

# Write your code below!
def compute_bill(food):
    total = 0
    for key in food:
        if stock[key] > 0:					# checks if there is stock
            total = total + prices[key]		# adds the price of fruit bought
                
	return total
    
print compute_bill(shopping_list2)

#4

that is working fine, what isn't working fine is where you reduce your stock. Which is now missing?

Also, a function ends the moment a return keyword is reached. So, currently, your function ends in the first run/iteration of your loop, but it doesn't match the error message you get?

When your code is finished, make sure to remove the function call, otherwise the stock will be altered before your code can be validated by the exercise

if you need more help, post an updated version of your code


#5

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

stock = {
    "banana"	: 6,
    "apple"		: 0,
    "orange"	: 32,
    "pear"		: 15
}

prices = {
    "banana"	: 4,
    "apple"		: 2,
    "orange"	: 1.5,
    "pear"		: 3
}

# Write your code below!
def compute_bill(food):
    total = 0
    for key in food:
        if stock[key] > 0:					# checks if there is stock
            total = total + prices[key]		# adds price of fruit bought
            stock[key] = stock[key] - 1		# reduces the stock inventory
            print total
    return total    

#compute_bill(shopping_list) 				# expecting $5.50
#compute_bill(shopping_list2)				# expecting $7

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.