-1

In the code below, it should be able to read the following input:

1
5 1
    N = int(input())
    for i in range(N):
        L, G = [int(s) for s in input().split(" ")]

Instead, I get the following error message:

Traceback (most recent call last):
  File "C:\Users\danie\PycharmProjects\googlecoding\main.py", line 32, in <module>
    L, G = [int(s) for s in input().split(" ")]
  File "C:\Users\danie\PycharmProjects\googlecoding\main.py", line 32, in <listcomp>
    L, G = [int(s) for s in input().split(" ")]
ValueError: invalid literal for int() with base 10: ''

For some reason, I do not get this error message if I first print something before the line with L, G. Any idea what goes wrong?

BrokenBenchmark
  • 13,997
  • 5
  • 12
  • 27
Harren
  • 1
  • 1
  • When you do not enter any characters, it gets read as an empty string '', which cannot be converted to int, hence the error. – Ach113 May 26 '22 at 21:17
  • Your very first `input()` reads the whole line. You have to parse that string. – Andras Deak -- Слава Україні May 26 '22 at 21:17
  • Of course with the updated question (thanks @BrokenBenchmark) the issue is probably something else. Please make sure your question's current rendered form represents your real problem. – Andras Deak -- Слава Україні May 26 '22 at 21:19
  • yes, the formatting went wrong - thanks. The updated question is the correct one. – Harren May 26 '22 at 21:20
  • So the problem is that you probably have a leading or trailing space: compare `'1 5 '.split(' ')` with `'1 5'.split(' ')`. You probably want `'1 5 '.split()` which will implicitly strip all whitespace from the edges. – Andras Deak -- Слава Україні May 26 '22 at 21:21
  • Thanks, but that gives the same error – Harren May 26 '22 at 21:25
  • OK. I ran your block of code (removing the 4 spaces of indentation) and pasted your first code block as input. I didn't get an error. Unless you can find what you're doing differently (either in your code or in the input) I won't reopen your question, because even though it's not a duplicate of what I chose earlier, it doesn't have a [mcve]. – Andras Deak -- Слава Україні May 26 '22 at 21:27
  • Note that multiple spaces e.g. `'1 5'` might also lead to the same error. – Andras Deak -- Слава Україні May 26 '22 at 21:28
  • How are you running this code? It works for me as posted and I am struggling to come up with a reason why a print before the input would make any difference. I ran in a linux terminal. – tdelaney May 26 '22 at 21:30
  • Hmm, I see now that if I simply copy and paste the two-line input it does work correctly - and in fact that is all I need. If I however, which I did first, first input 1 in the console, press Enter, and then input 5 1 and press enter, I got that error. So apparently that manual entering of the input per line does not work as I expected – Harren May 26 '22 at 21:36
  • It also works for me if I do what you described. Perhaps whatever terminal you are using is messing things up. To debug this, turn your list comprehension into a proper loop, and `print(repr(input_line))`, see what that looks like. – Andras Deak -- Слава Україні May 26 '22 at 21:41

0 Answers0