how to solve “EOFError: EOF when reading a line” error in python3?

删除回忆录丶 提交于 2020-01-25 10:10:49

问题


I'm totally new to python. I want to write a program which checks whether a number is squared or not. my code:

import math
T = int(input())

while T >= 0:
    num = int(input())
    sqrt = int(math.sqrt(num))

    if sqrt * sqrt == num:
        print('1')
    else:
        print('0')

    T = T - 1

the code is working correctly in my IDE(pycharm community 2017) but it gets a runtime error as you see in online IDEs (on geeksforgeeks ide):

Traceback (most recent call last):
  File "/home/043265f1cbdf257ecc20a7579588a4a4.py", line 5, in <module>
    num = int(input())
EOFError: EOF when reading a line

回答1:


Change it to:

while T > 0:

You are requesting 6 numbers if you compare for >= and your example only provides 5.

Maybe better:

import math

for _ in range(int(input)):
    num = int(input())
    sqrt = int(math.sqrt(num))

    if sqrt * sqrt == num:
        print('1')
    else:
        print('0')

and remove the T




回答2:


The value you're setting for T is 5, which means the while loop will run 6 times, but you're only providing 5 integers. That's why it tries to read an additional line and gives you the error.

So you should change the condition in your while loop to:

while T > 0:



回答3:


Yes, Change it to :

    while T>0:

you are only providing 5 values. EOF Error happens if no data is given to input(). It is also explained in the documentation



来源:https://stackoverflow.com/questions/49532525/how-to-solve-eoferror-eof-when-reading-a-line-error-in-python3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!