Page 122 -
P. 122
substring found
You were to adjust your code to use the extra logic technique to deal with lines that don’t contain
a colon character:
data = open('sketch.txt') It takes a few seconds to get your
head around this condition, but it
does work.
for each_line in data:
if not each_line.find(':') == -1:
Note the use of the (role, line_spoken) = each_line.split(':', 1)
“not” keyword, which print(role, end='')
negates the value of print(' said: ', end='')
the condition. print(line_spoken, end='')
data.close()
You were to think of any potential problems with this technique,
grabbing your pencil to write down any issues you might have
It’s OK if your issues with this approach.
are different. Just so
long as they are similar
to these.
There might be a problem with this code if the format of the data file
changes, which will require changes to the condition.
The condition used by the if statement is somewhat hard to read and
understand.
This code is a little “fragile”…it will break if another exceptional
situation arises.
86 Chapter 3