Why does my Python program give run time error on Kattis interpreter on Babelfish?

Question:

When I submit this file to Kattis I get a Run Time Error with no further explanation. It seems like simple enough code, but maybe I’m just missing something.

It runs on my python 3 interpreter. Why does it not work on Kattis? (or maybe other interpreter)

Problem: https://open.kattis.com/problems/babelfish

dictionary = dict()
userInput = input()
while userInput != "":
    buf = userInput.split()

    english = buf[0]
    foreign = buf[1]

    dictionary[foreign] = english
    userInput = input()


userInput = input()
while userInput != "":
    if userInput in dictionary:
        print(dictionary.get(userInput))
    else:
        print("eh")

    userInput = input()

Answers:

I think the problem is that the input data is not obtained with the input() function as you’re doing. You should read the standard input, like so:

for i in sys.stdin:
    ab = i.split()
    a = int(ab[0])
    b = int(ab[1])
    # Solve the test case and output the answer

Kattis documentation on Python3

Answered By: Felipe Ferri

Kattis recommends the sys.stdin to read the input data. However it is completely fine to use input() as well.

The breaking condition for the second while loop isn’t working in your code.

While there is an empty string telling you the first block is ending, there isn’t one for the 2nd block.

After the last data from the stream is caught by input(), you are still trying to get more data with userInput = input() in the next loop.
Thus throwing an EOFError, which you may catch to get a breaking condition:

while True:
    try:
        userInput = input()
    except EOFError:
        break
Answered By: caskuda
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.