[英]Return outside the function error:(Python)
Here, for this code I'm getting an error on return outside the function.在这里,对于此代码,我在函数外部返回时出错。 Can I anyone explain why is this so?
我能解释一下为什么会这样吗?
import numpy as np
name = input().split(" ")
arr = [int(num) for num in name]
sum=[]
for i in arr:
sum=+i
return sum
return
statement only makes sense inside functions. return
语句只在函数内部有意义。 To show or display infos use print(sum)
要显示或显示信息,请使用
print(sum)
You are using the return
keyword outside of a function.您在函数外部使用
return
关键字。 If you want to display the sum on screen use: print(sum)
.如果要在屏幕上显示总和,请使用:
print(sum)
。
You can't use the return
statement outside a function.不能在函数外使用
return
语句。 It is used to end the execution of a function and return the results of that particular function.它用于结束函数的执行并返回该特定函数的结果。 You can simply modify your script to a function like this.
您可以简单地将脚本修改为这样的函数。
import numpy as np
name = input().split(" ")
arr = [int(num) for num in name]
def function_name(array):
sum=[]
for i in array:
sum=+i
return sum
variable_name = function_name(arr)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.