B
B
babydick_danilko2020-06-15 11:06:14
Python
babydick_danilko, 2020-06-15 11:06:14

Why doesn't if text == line work even though the output is text = a, line = a?

If you write code like this, then everything is fine.

for line in phrases_read:
    if text in line.lower():
        ...

But if for the place text = a, line = a, it turns out to be text = a, and in line = car?
If "car" is the first line, and the letter "a" is the second, then the code will consider that "car" fits under if

If I write
if text == line.lower():
    ...

And if text = a, line = a, the code simply ignores this match
(translated text and line into a line, everything is the same. Reading comes from a txt file line by line using the permission "r")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pankov, 2020-06-15
@babydick_danilko

If the `in` operator has both string arguments, then this operator returns the occurrence of the substring in the string. The letter "a" is in the word "car" and there is nothing surprising here. You are very careless and inaccurate in your question. You probably do not understand how operators work and do not know that when reading from a file, paragraph characters remain in the lines.
Give the value of the lines in full. You may not notice a paragraph if you are typing a line. type repr(text), there will be visible non-printable characters.

And if text = a, line = a, the code simply ignores this match

This behavior is most likely due to either non-printable characters, or the letter you have written in different languages.
Please provide the full code. In its current form, without realizing it yourself (as, for example, in the case of non-printable characters), you may not attach importance to important things. You, there, are even too lazy to put quotation marks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question