Please help me dont know whts wrong in my code


#1

please tell me a solution whats wrong in my code i am copying my code here

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

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

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
def compute_bill(prices):
total=0
for key in prices:

  if stock[key] > 0:
     total = total+prices[key]
     stock[key]-1

  return total

and error screen shot:


#2

a function ends the moment a return keyword is reached, this currently happens in the first iteration of your loop

change the indent of return so the whole for loop can run


#3

i did changes as u told me but now it is giving following error
IndentationError: unindent does not match any outer indentation level


#4

can you copy paste an updated version of your code to the forum?


#5

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

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

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

    return total

#6

the indent of return should match one of the other indents. Which indent level should this be? it should still be inside the function but outside the loop


#7

when i changed my code it gives error SyntaxError: 'return' outside function
please help as soon as possible i am on same topic from last 2 hour
here is updated code:

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

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}
def compute_bill(prices):
  total=0
  for key in prices:
    
    if stock[key] > 0:
         total = total+prices[key]
         stock[key]-1

return total

#8

return should still be inside the function, but outside the loop

This means, the indent level should be the same as total=0 and for key in prices


#9

can u pls copy and paste that code here i will copy that and paste it in editor


#10

i can give you another example:

def example():
   print "hello world"
   if False:
       print "not true"
   return 0

see how i place return after the if statement? this way, return is reached regardless of if is true or false.

You should do the same for your for loop. return should be reached at the end of your function


#11

ok thanks for help..get it


#12