There are a few issues here but I'm guessing this is what's happening: you run the code, you enter the first two lines of input (4, followed by 3 20) and the code doesn't print anything so you press Enter again and then you get the results printed, but you also get the error.
Here is what's actually happening:
You enter the input and the program prints everything as you expect but, you can't see it. Because for some reason when you use sys.stdin
then print without a new line, stdout
does not flush. I found this similar issue with this (Doing print("Enter text: ", end="") sys.stdin.readline() does not work properly in Python-3).
Then when you hit Enter again, you're basically sending your program a new input line which contains nothing (equivalent to string of ""
). Then you try to split
that string which is fine (it will just give you an empty list) and then try to get the first element of that list by calling num[0]
but there are no elements in the list so it raises the error you see.
So you can fix that issue by changing your print statement to print(numbers, end = ' ', flush=True)
. This will force Python to show you what you have printed in terminal. If you still try to Enter and send more empty lines, however, you will still get the same error. You can fix that by putting an if inside your for loop and check if line == ""
then do nothing.
There is still an issue with your program though. You are not printing new lines after each line of output. You print all of the numbers then you should go to the newline and print the answer for the next line of input. That can be fixed by putting a print()
outside the for loop for numbers in range(num[0], num[-1] + 1):
.
That brings us to the more important part of this answer: how could you have figured this out by yourself? Here's how:
Put logs in your code. This is especially important when solving this type of problems on SPOJ, Codeforces, etc. So when your program is doing that weird thing, the first thing you should do is to print your variables before the line of the error to see what their value is. That would probably give you a clue. Equally important: when your program didn't show you your output and you pressed Enter again out of confusion, you should've gotten curious about why it didn't print it. Because according to your program logic it should have; so there already was a problem there.
And finally some side notes:
- I wouldn't use
eval
like that. What you want there is int
not eval
and it's just by accident that it's working in the same way.
- We usually put comments above the line not below.
- Since it appears you're doing competitive programming, I would suggest using
input()
and print()
instead of stdin
and stdout
to avoid the confusions like this one.
- SPOJ and other competitive programming websites read your stdout completely independent of the output. That means, if you forget to put that empty
print()
statement and don't print new lines, your program would look fine in your terminal because you would press Enter before giving the next input line but in reality, you are sending the new line to the stdin
and SPOJ will only read stdout
section, which does not have a new line. I would suggest actually submitting this code without the new line to see what I mean, then add the new line.
- In my opinion, the variable
numbers
does not have a good name in your code. numbers
imply a list of values but it's only holding one number.
- Your first if does not need to start from 1 and go to the number does it? It only needs to iterate that many times so you may as well save yourself some code and write
range(int(input()))
.
- In the same for loop, you don't really care about the value of variable
x
- it's just a counter. A standard practice in these situations is that you put _
as your variable. That will imply that the value of this variable doesn't really matter, it's just a counter. So it would look like this: for _ in range(int(input()))
.
I know some of these are really extra to what you asked for, but I figured you're learning programming and trying to compete in contests so thought give some more suggestions. I hope it helped.