1

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

2
  • 1
    you can't use return outside a function. Commented Oct 22, 2020 at 10:13
  • 1
    As stated in the error message, you can't have return outside a function. What would you expect this to accomplish here? Commented Oct 22, 2020 at 10:14

3 Answers 3

2

return statement only makes sense inside functions. To show or display infos use print(sum)

Sign up to request clarification or add additional context in comments.

Comments

1

You are using the return keyword outside of a function. If you want to display the sum on screen use: print(sum).

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.