What is wrong in my code? Works fine on my machine.

I am doing very first practice question. And I have choose Python as language. Here is the code:

lists = [1, 2, 88, 42, 99]

for numbers in lists:
    if numbers == lists[3]:
        break
    print(numbers)

Well this works fine on my system, but I don’t know why isn’t it working on CodeChef.

Edit:

Well I understand what I was doing wrong, by keeping that in mind I did this:

prompt = None

while prompt != 48:
    prompt = int(input())

This works fine for me, but gives Runtime Error on CodeChef. What’s the problem?

Welcome to codechef!

Here you are supposed to solve problems by writing code. And if you know the input and output as well, do you really need to code? Your code is supposed to take arbitrary input through standard input (STDIN), solve the problem and produce output on standard output (STDOUT).

Now what you type through keyboard is STDIN, and what you see on the screen is STDOUT.

What you have coded is not solving the problem. Read the problem statement carefully. It says ->

rewrite small numbers from input to
output. Stop processing input after
reading in the number 42.

So what you need to do is, run a loop, which accepts integer as input. Checks if it is 42. If yes, finish the program, otherwise print the number, and continue in the loop.

For more clarity->

The numbers 1, 2, 88, 42, 99 are for example only. This is not what your code will be run against. They are there just to ensure you understand the problem. So instead of storing them in list, you need to read the input.

While this is the easiest problem, and I (or anyone) can give you the code, it is better if you come up with solution yourself.

EDIT - Whenever you are unable to solve any problem, re-read the problem carefully again and again, until you understand it. The problem statement says to stop on 42, and not 48. Also you are supposed to print all the numbers, read prior to 42.

4 Likes

You are trying to iterate till you find 48.

The question clearly says, you need to parse the input till you find 42 (which is surely there in the input).

If you change 48, to 42 in your code. You get Wrong Answer because you are not actually printing 42.

That said, why did your program give a run time error with 48? One reason I can fathom is that the test file did not contain the number 48, because of which python threw an exception, which looks something like below

Traceback (most recent call last):
  File "test.py", line 4, in 
    prompt = int(input())
  File "", line 0
    
    ^
SyntaxError: unexpected EOF while parsing

This is indeed a RuntimeError according to Python.

See my updated question.