Lists + Functions vs. Something of Value


The code I posted here is correct, but I am confused when should I put 'RETURN' in the loop.

When I do the loop for something of value, I typed out return total in the loop just like return count in the lists+function exercise and it turned out an error. Can anyone explain when I should use 'RETURN' in the loop? Thank you.

def fizz_count(x):
    for item in x:
        if item=="fizz":
    return count

print fizz_count(["fizz","cat","fizz"])

# Something of Value
prices = {
    "banana" : 4,
    "apple"  : 2,
    "orange" : 1.5,
    "pear"   : 3,
stock = {
    "banana" : 6,
    "apple"  : 0,
    "orange" : 32,
    "pear"   : 15,

for key in prices:
    print key
    print "price: %s" % prices[key]
    print "stock: %s" % stock[key]
for key in prices:
    total= total+prices[key]*stock[key]
print total


To me, this looks like it's not in the loop judging by the indention.
The return should always be outside of a loop as, when used, it exits the loop. If the return was inside of the loop, the loop would run until it hits 'return', and the loop will end right there. Returning whatever value it was told to return at that instance.

Hope this helps! If not, feel free to reply and I'll try to go more in-depth.


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