Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime error with python code online, works offline

The code runs successfully offline, however, when uploaded to a code challenge site it gives me a RuntimeError. I am unsure why it is doing this.

My code:

inputinteger = int(input(""))
N = inputinteger

inputinteger2 = int(input(""))
M = inputinteger2

A = int(N / M)

if (N % M == 0):
    B = 0
    print("masing-masing " + str(A))
    print("bersisa " + str(B))

else:
    B = int(N % M);
    print("masing-masing " + str(A))
    print("bersisa " + str(B))

Note: "masing-masing" in English means "each". "bersisa" in English means "remainder".

Inputting some test input 15 and 3 gives the expected result. The code however does not work when run online.

Thank you for your help!

like image 935
giffaro97 Avatar asked Apr 23 '19 02:04

giffaro97


2 Answers

Adding to the answer above.

The online challenge site is not or may not be supplying the input required for the code to run. I will suggest you simply replace either input() or raw_input() with actual values that the system can work with.

like image 147
anabeto93 Avatar answered Oct 13 '22 06:10

anabeto93


I am not 100% positive on this, but it is a best guess and there's not enough room in the comments.

For python 2, it may be that you are meant to use raw_input instead of input in your code. That being said upon testing python 2 code, it seems a lot of online interpreters don't work with input, or raw_input anyway. See here: https://www.tutorialspoint.com/execute_python_online.php and here: https://paiza.io/en/projects/new?language=python. If you put in this code:

a = raw_input()

It will fail with an EOF error. I believe this is because these interpreters don't actually allow for input.

if you change the lines inputinteger = int(input("")) and inputinteger2 = int(input("")) to just inputinteger = 4 and inputinteger2 = 5, I think you'll find that you no longer get a run time error.

Since you said English is your second language, if that was hard to understand, here it is in a bit simpler terms:

Some old python versions do not use input. They use raw_input. Try replacing input with raw_input.

If that does not work, maybe the online python does not let you use input or raw_input. Try using code with no input.

EDIT:

based on how the website makes it seem you will be receiving your input, I would try changing your code to the following:

inputinteger, inputinteger2 = raw_input("").split(" ")
N=int(inputinteger)
M=int(inputinteger2)

A=int(N/M);

if(N%M==0):
    B=0
    print("masing-masing "+str(A))
    print("bersisa "+str(B))

else:
    B=int(N%M);
    print("masing-masing "+str(A))
    print("bersisa "+str(B))

If that doesn't work, I'm sorry but I'm completely out of ideas as to why it might be failing. I've tested with the example input they gave on the website and it works perfectly. We know you have the right answer, if it means anything..

Edit: Further explanation:

So the website said you would be receiving input in the following format (as an example):

15 3

This means, you only received a single string. The line:

inputinteger, inputinteger2 = raw_input("").split(" ")

Is taking in that single string, and using .split(" ") to split it into different values (stored in a list) on either side of the empty space. So essentially, it assigns

inputinteger, inputinteger2 = ["15", "3"]

Which gives you the appropriate values for those variables. From there, your old code works perfectly.

like image 21
Recessive Avatar answered Oct 13 '22 05:10

Recessive