Back to Problem Solutions forum
Hello to everyone from this fantastic site!
As the title says I have trouble getting the input from Test case.
The first thing I fetch is, of course, the first number:
n = int(input())
So far so good :P. But the problem comes later, the format of the lines is <sign> <integer>
, so I tried to get
them in several ways, but I had no success...
I tried unpacking the input directly, as they are separated with a blank space: for sign, value in input().split(' '): value = int(value) The exception here said it couldn't unpack because it expected two values but got one.
I tried to do the same in two steps: for line in input(): sign, value = line.split(' ') Same exception as the former, sure enough, they're the same.
I also tried fetching the parts as substrings:
for line in input():
sign = line[0]
value = int(line[2:])
I got ValueError: invalid literal for int() with base 10: ''
But if I tried the former way without looping, I mean taking just the first line (after the lonely number), I have success.
line = input()
sign = line[0]
value = line[2:]
print("{} {} ({})".format(sign, value, len(value))
It even works if I convert value into int, but when I loop It raises the same exception. I thought there was an error on one of the inputs to take, but it makes no sense because they're generated automatically, aren't they? And I even check its length, since there may be an invisible character I get which doesn't allow me to parse the string into an int.
What is the probleme here? I'm unable to grasp it...
Any help would be appreciated :)
Gabriel
Gabriel Hi!
I think your idea with split
was correct. If it fails - you'd better debug and find out why it fails for you (and fix
this). For example you probably are hitting the end of file. Thus you shouldn't make for line in input
but rather
use for i in range(n)
.
Alternatively you can just gracefully catch the input error when the end is met.