Ask coding questions

← Back to all posts
Why isn't word in word_list?
AIC27 (0)

When I run the code, it runs as if 'core\n' (the value of word) and 'core\n' (the value of a word_list item) aren't the same. What am I doing wrong?

Answered by m4rus3ru (15) [earned 5 cycles]
View Answer
Voters
AIC27 (0)
Comments
hotnewtop
m4rus3ru (15)

That's because in your list there is core\n, but not 'core\\n', which is the content of your variable word.
word should be like this:

word = base_word+"\n"
AIC27 (0)

@m4rus3ru The second backslash needs to be there. The "\n means a new line, and the first backslash is an escape character. The value of a string with two backslashes and an n is "\n"

m4rus3ru (15)

@AIC27 No. When you read the line it gets stored as [word]\n in the variable, so when you want to check if the list contains your word then your variable literally needs to be [word]\n as value too. If you escape the escape char your variable will hold the value [word]\n.
Your issue is that you did escape it in the variable you check with, but not in the word list.
There is no reason for you to escape it and I guess you misunderstood in which case you do need to escape something.

Just test what I have said and you will notice that it is the solution to your problem.

AIC27 (0)

@m4rus3ru I get it now. I thought that because it was being displayed as a string, the escape character was included in the value. Thank you for your explanation.