Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I am working on the online platform and the code I am using is as follows:

#Given an array of integers, find the sum of its elements.

#For example, if the array , , so return .

#Function Description

#Complete the simpleArraySum function in the editor below. It must return the sum of the array elements as an integer.

simpleArraySum has the following parameter(s):

ar: an array of integers

#Input Format

#The first line contains an integer, , denoting the size of the array.

#The second line contains  space-separated integers representing the array's elements.

#Constraints

#Output

#Print the sum of the array's elements as a single integer.

So I tried on the code on the jupyter notebook and it gives the error:

Traceback (most recent call last):

  File "Solution.py", line 34, in <module>

    result = simpleArraySum(ar)

  File "Solution.py", line 13, in simpleArraySum

    amount=int(input())

EOFError: EOF when reading a line

This is the code which I have used:

def simpleArraySum(ar):

    #

    # Write your code here.

    #

    amount=int(input())

    nums=list(map(int,input().split()))

    sums=0

    for i in nums:

        sums+=i

    print(sums)

Can anyone help me solve this?

1 Answer

0 votes
by (36.8k points)

As per my knowledge, you can use the below code to solve the issue:

def sum_array(arr):

    total = 0

    for i in arr:

        total += i

    return total

These are the errors which I found in your code:

you are passing array and solve the inputting the values which are not correct. anyone should be used 

use cast the integer and later you split the input

You can use the custom function to code instead of coding from scratch.

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

Browse Categories

...