1

I have this bit of python code for the Left Beehind problem but i keep getting compile error and no information as to why:

def decision(sweetJars, sourJars):
    if sweetJars == sourJars == 0:
        return
    if sweetJars + sourJars == 13:
        print("Never speak again.")
    elif sweetJars > sourJars:
        print("To the convention.")
    elif sourJars > sweetJars:
        print("Left beehind.")
    else:
        print("Undecided.")
        

for _ in range(int(input())):
    
    values = input().split()
    a = int(values[0])
    b = int(values[1])
    decision(a,b)

I'm using Python 3. What am i getting wrong here?

5
  • 1
    What error? The Python interpreter tells you what the problem is and where it occurred.
    – ndc85430
    Sep 22 at 6:10
  • 1
    Kattis website doesn't specify it just returns Run-Time Error. Thats the problem theres no feedback on what the issue is
    – WDUK
    Sep 22 at 6:48
  • Have you tried to run the program locally to see if you get an error?
    – ndc85430
    Sep 22 at 6:52
  • please specify what input you expect for the second input promt and put it in the __promt argument of input`
    – Jan_B
    Sep 22 at 6:58
  • edited my answer to actually tackle the compile error (which I did not know, since I am not familiar with Kattis). Please check if the first segment is helpful.
    – Jan_B
    Sep 22 at 8:17

1 Answer 1

1

First: Kattis specifies this error here:

Compile Error means that we failed to compile your source code. In order to help you debug the error, the compiler output is available as extra information. Information about what compilers and flags are used can be found on the help pages for each language.

And on the help page:

Now that we have determined a suitable type, we just have to read the data. Reading is done from standard input. In this problem, we should read until the end of the file (in other problems, there might be an integer at the beginning of the input, specifying how much to read, or there might be a special indicator denoting that there is nothing more to read). Using sys.stdin, this can be done as below:

I edited the sample from the site to fit your script:

# decision() method stays unchanged

for i in sys.stdin:
    values = i.split()
    a = int(values[0])
    b = int(values[1])
    decision(a,b)

So this is how you have tho change your loop.


Second: running your code on my machine (windows 10 / python3) does not return an error ecept if the input values given to the script are not as expected. But if you e.g. input a single digit for the second promt you get IndexError: list index out of range because you cant split a string with a single character...

So aside from Kattis (which I do not know or use) I recommend implementing some kind of input validation for the start() mehtod:

def decision(sweetJars, sourJars):
    if sweetJars == sourJars == 0:
        return
    if sweetJars + sourJars == 13:
        print("Never speak again.")
    elif sweetJars > sourJars:
        print("To the convention.")
    elif sourJars > sweetJars:
        print("Left beehind.")
    else:
        print("Undecided.")


def start():
    inp = input('Enter number of tries:')
    # this try block validates the input can be cast as int
    try:
        rounds = int(inp)
    except ValueError:
        if inp.lower() == 'q':
            print('Script stopped!')
            return
        print('Please input a positive integer or "q" to quit!\n')
        start()
    else:
        # also check if the number is positive (optional)
        if rounds <= 0:
            print('Please input a positive integer or "q" to quit!\n')
            start()
        else:
            i = 0
            # while loop, so loop iterations can be repeated until input is correct
            while i < rounds:
                inp = input('Enter two Numbers seperated by space:')
                values = inp.split()
                # validate if the sting was split (by checking the second element first) and both parts are int
                try:
                    b = int(values[1])
                    a = int(values[0])
                except ValueError:
                    print('Both values have to be integers\n')
                except IndexError:
                    if inp.lower() == 'q':
                        print('Script stopped!')
                        return
                    print('Please input the values in the format: "X X"\n')
                    # only increment i if input was correct
                else:
                    decision(a, b)
                    i += 1
                    print(f'\nTry {i} of {rounds}.\nTo quit early enter "q"')


if __name__ == '__main__':
    start()

But this is propably overkill for the Kattis problem...

1
  • Kattis always gives valid input--the goal is more the coding problem/algorithm than writing robust software, although the sentiment is appreciated!
    – ggorlen
    Oct 9 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.