当我试图输入一个程序,试图在数组中找到一个数字的位置时,我会得到一个错误,我想一次给出整个输入,怎么做呢,有更简单的方法吗?谢谢。当试图在pycharm中的控制台中执行时,我会得到以下错误
示例
输入
2
4.
1 2 3 4
3.
5
10 90 20 30 40
40
# Returns index of x in arr if it is present,
# else returns -1
def search(arr, x):
n = len(arr)
for j in range(0,n):
if (x == arr[j]):
return j
return -1
# Input number of test cases
t = int(raw_input())
# One by one run for all input test cases
for i in range(0,t):
# Input the size of the array
n = int(raw_input())
# Input the array
arr = map(int, raw_input().split())
# Input the element to be searched
x = int(raw_input())
print(search(arr, x))在pycharm控制台上我的输入:
2
4
1 2 3 4
3
5
10 90 20 30 40
40 输出
Traceback (most recent call last):
File "<input>", line 8, in <module>
ValueError: invalid literal for int() with base 10: '2\n 41 2 3 4\n 3\n 5\n 10 90 >20 30 40\n 40'发布于 2016-09-22 11:35:47
输入字符串不能转换为int ('2\n 41 2 3 4\n 3\n 10 90 >20 30 40\n 40') --您应该用'\n‘和’int‘解析输入值,然后将其转换为int数。
https://stackoverflow.com/questions/39637634
复制相似问题