[每当我尝试运行代码时,它都会显示运行时错误。如何解决。(Hackerrank)

问题描述 投票:-1回答:1

我是这里的新手,并且是Hackerrank。我正在尝试解决简单的数组和问题:

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 Format

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

我正在尝试找到解决方案,但到目前为止我找不到解决方案。它在jupyter笔记本上有效。它说:

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

在输出按钮上说'stdout没有响应。这是我的代码:

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)
python runtime stdout eoferror
1个回答
0
投票

您可以这样做;

def sum_array(arr):
    total = 0
    for i in arr:
        total += i
    return total

您的代码存在一些问题。

  • 您希望将数组传递给函数,但还希望用户输入值。
  • 您将输入转换为整数,但是在拆分整数之后,整数不是序列类型。

请注意,有许多方法可以解决此问题,而无需编写您自己的自定义函数。

© www.soinside.com 2019 - 2024. All rights reserved.