Skip to content Skip to sidebar Skip to footer

Why Does Reading A Line In Python Not Give Me Just The Contents Of That Line?

I'm having trouble understanding the correct variable in the next_block(the_file) function. The program won't work correctly if the correct variable is not indexed. Hence, correct[

Solution 1:

After correct = next_line(the_file), correct is a string like '1\n'. correct[0] then gets you a string like '1', which you later compare to the result of raw_input, which doesn't include a \n at the end. So you need to do [0] to get the first character out.

It would probably be better to use .strip() instead, because then it would potentially work for answers that aren't a single character (if you changed the game to support 10+ answers, or answers with a different kind of name), it'd be a little more obvious what's going on, and it would ignore spaces on the ends, which are definitely irrelevant in either the file or the user's input.


Post a Comment for "Why Does Reading A Line In Python Not Give Me Just The Contents Of That Line?"