Making a puchase


#1

shopping_list = ["banana", "orange", "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:
        total = total + prices[key]
stock[key] = stock[key] -1
return total

please help me
File "python", line 24
SyntaxError: 'return' outside function


#2

this is currently your function + content:

def compute_bill(food):
    total = 0

pyhon uses indention to determine nesting (so what is nested inside the function)


#3

Indentation is important in Python, you need to make sure everything is formatted correctly in your function:

def myFunction():
    print "this is inside the function"
print "this is outside the function"

#4

even when I put the indent it its say unexpected indentation


#5

Can you post your updated code?


#6

I just put on line 24 because that's what it says in the console
return total


#7

with and indent with it of course


#8

Okay, but you need to consider the rest of the function - is the for loop part of the function too? If so, it should also be indented correctly.

def myFunction():
    print "This is inside the function"
print "This is outside the function"
    return "This won't work"

I'm guessing this is similar to what you've done. It won't work because the return statement is no longer part of that function, as you've already closed it off with incorrect indentation.

Everything that is part of a function should be nested inside it using indentation.

def myFunction():
    print "This is inside the function"
    print "This is also inside the function"
    return "This will work"

#9

ok so all of them should be indented like so

def compute_bill(food):
    total = 0
    for key in food:
    if stock[key] > 0:
total = total + prices[key]
stock[key] = stock[key] -1
    return total

#10

so the for loop gets it
if stament
and the return total


#11

to nest something inside a for loop or if statement, you also need indent:

if condition:
     print "inside the if clause"
print "outside the if clause"
if another condition:
print "simply invalid, something needs to be inside the if clause"

#12

sorry sir I really cant figure this problem out it just something I'm not seeing


#13

You might be a getting a little confused with how to indent properly. In Python, indentation is used (for example) for functions, for loops and if statements to denote which lines belong to which blocks of code:

if x = y:
    print "x equals y"

for x in y:
    print "the value of x is: " + x

def myFunction():
    print "this is a function"
    return true

In all of the above examples, the indentation indicates that the lines below are part of those code blocks.

You can also combine them together:

def myFunction():
    if x = y:
        print "x equals y"
        print "this is inside the if statement"
    print "this is inside the function"
    return true

This is all one function, but notice how the indentation still applies to the if statement inside it.

So in your code, you need to make sure that everything inside the function is indented, but that any code blocks (such as for loops and if statements) are appropriately indented.


#14

[quote="azere, post:9, topic:149369"]

def compute_bill(food):
total = 0
for key in food :
print key
if stock[key] > 0:
total = total + prices[key]
stock[key] = stock[key] -1
return total


#15


#16

Okay, better but still not finished; the stock[key] - 1 line should be indented to sit inside the if statement (since it should only execute if stock[key] is greater than 0.

You also still need to indent the return statement so that it sits inside the function.


#17


#18

this code is for the next exercise, in this exercise you shouldn't yet check if item is in stock


#19

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