我不熟悉这里和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
在输出按钮上,它显示“标准输出无响应”。以下是我的代码:
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)
这应该适合您:
问题:您只有一个问题:
1.您只需对给定数组的所有元素求和,只需求和,您不需要读取用户的输入。这就是我在您的代码中所做的更改,然后删除所有内容并在修改后编写代码,应该可以正常工作,一切都会很好
这应该很好用。只需复制此代码并粘贴即可:
我什么也没做,只是读取函数外部的输入
你可以这样做
您的代码存在一些问题
请注意,有许多方法可以在不编写自定义函数的情况下解决此问题
相关问题 更多 >
编程相关推荐