+ 5
In the Code Coach Challenges the length of the input seems to be longer than the actual input.
When i read the input in python using something like: Word = input() or words = input().split() The string named word or the last element in words is 1 longer than the actual word. This does not seem to be the case in the code playground. Does anyone know why this is and how to avoid this?
19 odpowiedzi
+ 8
You can use input().strip(), that removes any invisible whitespace like \n or \r at the edges and doesn't do harm if there wasn't any.
+ 3
davy hermans yes it is the return carriage /r at the end of some strings. You can check this with a print of the string as your first output in Code Coach chalenge cases 1 and 2.
+ 2
davy hermans I guess in Code Coach the input is automated hence the /r problem. I got the same issue.
+ 2
HonFu
Just to wrap it up. You were right. I've confused strip with split from your first post. Second your code bit from your other post I was expecting to return chars one by one because I was inputing a string of only one word no space that is why I said it is not working.
davy hermans sorry for highjacking your post.
HonFu constructive discution, at least from my side, hope I didn't offend you in any way.
+ 1
Mihai Apostol thanks for your answer.
I saw the /r but i wonder why i don't see this in the code playgound. Is it because of the way the input is presented?
+ 1
HonFu thanks, that is exactly what i was looking for.
+ 1
HonFu thou...check this two codes:
x = input()
for char in x:
print(char)
x = input().split()
for char in x:
print(char)
+ 1
Mihai Apostol if i am right, the first one is a string, the second a list.
+ 1
davy hermans it is the other way around. With the first one I was able to go through the chars one by one with the second I cannot.
+ 1
HonFu just ran it in playground...not working...
+ 1
HonFu a string
+ 1
HonFu I know what it doesn't do. It is not going through the chars one by one.
+ 1
You said:
"You can use input().strip(), that removes any invisible whitespace like \n or \r at the edges and doesn't do harm if there wasn't any."
I said:
"HonFu thou...check this two codes:
x = input()
for char in x:
print(char)
x = input().split()
for char in x:
print(char)"
You said that using split doesn't do any harm, I just said I cannot go through the string one char at a time.
It is on topic, as I was interesed to remove those \r at the end of some strings in code coach.
+ 1
strip - not split - is what I said to be not doing any harm.
Split returns a list of strings, and strip only works with strings.
So you have to use strip on each word in the list - which my code example does.
What exactly is 'not working' with it? (I tried it - does just what I said.)
+ 1
Hum..
0
Mihai Apostol,
x = input().split()
for word in x:
print(word.strip())
0
Mihai Apostol What did you input?
0
Do you understand what that piece of code is supposed to do?
0
It's not the point of these lines to do that, so it would be strange if they did.
What exactly do you want to know?
And has it a relation to this question? (If not, you should make a post of your own.)