What does this error mean and how to fix it: Eoferror: EOF when Reading a line?

Asked

Viewed 341 times

0

The code I was testing worked perfectly in the visual studio, however, by submitting on one of these sites similar to the URI, that error appeared. I know it is a line reading error, but I could not understand why it is working in visual studio and this site does not work. The code:

def primos_gemeos(num):
    valor = False
    for n in range(2, num):
        if num % n == 0:
            break
    else:
        valor = True
    return valor

quantidade = int(input())
lista = []
proxPrimo = None
n = 3
atualPrimo = primos_gemeos(n)
while True:
    proxPrimo = primos_gemeos(n + 2)
    if atualPrimo and proxPrimo:
        lista.append((n, n + 2))
        if len(lista) == quantidade:
            break
    atualPrimo = proxPrimo
    n += 2

print(lista)
  • What will be the entry of the program?

  • Could be any integer

  • You don’t have to fix it. Depending on where you tested you have to provide the input(s) before running.

  • The else do if you are getting it right? You are trying to submit on those sites like UVA online and URI online?

  • That’s right, like these sites and I believe that Else is not the problem, because I tested another approach to check if a number is prime

  • 1

    @Danizavtz Python has the structure else to the for also, not only for the if.

Show 1 more comment

1 answer

3

Apparently you are not dealing with the exception of the entry. In the URI platform the exception is triggered EOFError when the program entry is completed.

while True: 
    try: 
        #seu código entra aqui 
        ...
    except EOFError:
        break

Browser other questions tagged

You are not signed in. Login or sign up in order to post.