繁体   English   中英

为什么for循环返回None?

[英]Why does the for loop return None?

我正在Code Academy进行练习,以遍历shopping_list 为什么以下代码结果在结果中返回额外的None

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))

您需要使用return语句来获取函数compute_bill的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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