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