[Tutor] Question
Peter Otten
__peter__ at web.de
Thu Aug 21 10:04:27 CEST 2014
Lucia Stockdale wrote:
> Hi everyone,
>
> 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.
In the future please include the traceback (cut and paste, don't rephrase).
> This is my goal:
>
> Line: hello world
> olleh dlrow
> Line: extra
> artxe
> Line: i love python
> i evol nohtyp
> Line:
>
> This is my current code:
>
> line = input('Line: ')
> while line != '':
> line = line[len(line):0:-1]
Python uses half-open intervals; thus line[0] is not included in the result.
Use
line = line[::-1]
to reverse the string.
> line = line.split()
> line = line.reverse()
The reverse() method modifies the line in place, and by convention such
methods return None in Python. Change the above line to
line.reverse()
> line = (' '.join(line))
> print(line)
I see another problem, the while loop will run at most once, but you should
be able to fix that yourself.
More information about the Tutor
mailing list