Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么for循环返回None?_Python - Fatal编程技术网

Python 为什么for循环返回None?

Python 为什么for循环返回None?,python,Python,我正在代码学院做一个练习,反复浏览购物清单。 为什么以下代码结果在结果中返回额外的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

我正在代码学院做一个练习,反复浏览购物清单。 为什么以下代码结果在结果中返回额外的
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)

您需要使用
return
语句来获取函数
compute\u bill

的结果。您的函数将返回
None
。也许您的环境只是在函数运行后显示返回值#在下面编写代码!def compute_bill(food):total=0表示食品中的x:print x total+=prices[x]返回total print“total:+str(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))