简体   繁体   中英

Why does the for loop return None?

I'm working an exercise from Code Academy to iterate through the shopping_list . Why does the following code result return an extra None in the result?

shopping_list = ["banana","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 x in food:
        print x
        total += prices[x]
compute_bill(shopping_list)
shopping_list = ["banana","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 x in food:
        print x
        total += prices[x]
    return total
print(compute_bill(shopping_list))

You need use return statement to get the result of the function compute_bill .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM