Python函数执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34492510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python function execution
提问by RaviTej310
So i found this site called codewars.com and wanted to signup. When i selected my language as python, it gave me a problem to solve:
所以我找到了这个名为 codewars.com 的网站,并想注册。当我选择我的语言作为 python 时,它给了我一个需要解决的问题:
The code does not execute properly. Try to figure out why.
代码没有正确执行。试着找出原因。
def multiply(a, b):
a * b
I am not able to figure out why. It executes correctly in PyCharm using python 3.4 when i added print(a*b)
instead of a*b and when i called the function using multiply(2,3)
. The code is also being successfully executed using just the given snippet. It has been given that the above python code is in 2.7 Any ideas?
我不知道为什么。当我添加print(a*b)
而不是 a*b 并且当我使用multiply(2,3)
. 仅使用给定的代码片段也可以成功执行代码。已经给出上述python代码在2.7中的任何想法?
采纳答案by fedorqui 'SO stop harming'
If it is a function, it needs to return something. Otherwise, running it is kind of useless.
如果它是一个函数,它需要返回一些东西。否则,运行它是没有用的。
So you probably need to say:
所以你可能需要说:
def multiply(a, b):
return a * b
You probably want to read more about functions in Python and when this would make sense (passing by reference, for example). This can be a good starting point: Python functions.
您可能想阅读有关 Python 中函数的更多信息以及何时有意义(例如,通过引用传递)。这可能是一个很好的起点:Python 函数。
回答by yanghaogn
There is no return value, the code will be OK
没有返回值,代码就OK了
def multiply(a, b):
return a * b