2017-06-22 56 views
1
N = int(input()) 

list = [] 

while(N>0): 

    n = int(input()) 
    list.append(n) 
    N = N-1 

Q = int(input()) 

while(Q>0): 

    check = int(input()) 
    count = list.count(check) 
    if(count==0): 
     print("NOT PRESENT") 
    else: 
     print(count) 
    Q = Q-1 

以上是我在hackerearth中编写的代码。我作为运行时错误得到响应。但是,当我在我的Ubuntu的Python控制台尝试它的作品完美HackerEarth运行时错误 - Python 3中的NZEC

+0

我不知道什么HackerEarth ......但如果他们的解释是蟒蛇2(而不是python3)你可以尝试使用的raw_input'来代替'输入()'()'。 –

+0

错误是什么? – Arun

回答

0

根据提供的信息,它看起来像你试图解决this problem

输入数组是所有提供在一行中的元素列表。你的代码使用'input()',它一次读取整行。但是,由于存在空格字符,因此无法将其转换为整数。

罪魁祸首代码: -

N = int(input()) 
list = [] 
while(N>0): 
    n = int(input()) 
    list.append(n) 
    N = N-1 

更改为: -

n = int(input()) 
arr = [int(x) for x in input().split()] 

此外,我会建议不要使用变量list,因为它过度乘坐该变量/类型的全球意义。

既然你说过它在你的本地机器上工作,我认为你是手动输入输入。测试本地工作的正确方法是将示例输入写入文件,例如input.sample。然后运行命令行下面的命令(S)(我假设你正在使用的* nix系统和蟒蛇代码文件code.py中)

$> python3 code.py < input.sample 

这应该给你的错误: -

Traceback (most recent call last): 
    File "/tmp/test2.py", line 7, in <module> 
     n = int(input()) 
ValueError: invalid literal for int() with base 10: '1 1 1 2 2 0'