[Tutor] Question
Felix Dietrich
felix.dietrich at sperrhaken.name
Thu Aug 21 10:08:16 CEST 2014
Lucia Stockdale <Lucia.Stockdale at mggs.vic.edu.au> writes:
> I have been writing a program to print words backwards until an an
> empty line of input is entered, but after I put in the input it comes
> up with TypeError.
For completeness give us the whole Traceback, it makes pin-pointing the
error easier. Also, tell us which Version of /python/ you are using. I
assume you are using python3, because you did not get a SyntaxError in
/input/ (in python2 /input/ evaluates the string as python-code).
> This is my current code:
>
> line = input('Line: ')
> while line != '':
> line = line[len(line):0:-1]
> line = line.split()
> line = line.reverse()
Reverse mutates the list /line/ and returns None, which you then assign
to /line/. Returning None is a common pattern for python methods mutating
the objects value and not creating a copy.
> line = (' '.join(line))
Your calling of
' '.join(None)
results in a TypeError here. str.join takes an iterable.
> print(line)
After the call to /print/ you probably want to duplicate the line
line = input('Line: ')
or you will end up in an infinite loop, since your program does not read
another line of input and therefore the value of /line/ never changes
and gets no chance to become the empty string.
--
Felix Dietrich
More information about the Tutor
mailing list