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. To show or display infos use print(sum)
You are using the return
keyword outside of a function. If you want to display the sum on screen use: print(sum)
.
You can't use the return
statement outside a function. 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)
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.