[Tutor] Question

Cameron Simpson cs at zip.com.au
Thu Aug 21 09:55:17 CEST 2014


On 21Aug2014 13:03, Lucia Stockdale <Lucia.Stockdale at mggs.vic.edu.au> wrote:
>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.

Hi Lucia,

It would be helpful to include the whole traceback that Python shows you. Can 
you reply including this information please?

At the least it should tell you the precise line on which the error occurs.  
Then you can insert a print() call before that particular line to show the 
values or types involved.

[...snip...]
>Line: i love python
>i evol nohtyp
[...]

Thanks for providing a nice clear description of the desired output.

>This is my current code:
>line = input('Line: ')
>while line != '':
>  line = line[len(line):0:-1]

You might find:
   line = line.strip()
simpler.

>  line = line.split()

"line" used to be a string. Now it is an array of strings. I tend to write this 
as:

   words = line.split()

and work with the name "words" from there on. It keeps it clearer in the mind 
the different types of values in play.

Anyway, come back wish a traceback and the results of any other experiments you 
make with your program and we'll see where we are.

Cheers,
Cameron Simpson <cs at zip.com.au>

... and that was only one of many occasions on which I met my death,
an experience which I don't hesitate to recommend strongly.
         - Baron von Munchausen


More information about the Tutor mailing list