[Tutor] Question

Dave Angel davea at davea.name
Thu Aug 21 10:32:31 CEST 2014


Lucia Stockdale <Lucia.Stockdale at mggs.vic.edu.au> Wrote in message:
> Hi everyone,

Welcome to the list. And thanks for using text mode.

What's the Python version and os version.  I expect this is crucial.

> 
> 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.

What else did it say? You're paraphrasing a traceback into one
 word. Just copy/paste the whole thing, starting with the word
 Traceback,  and ending with the error line, apparently starting
 with the word TypeError:

For that matter, you should include the output starting with the
 prompt 'Line: '

> 
> 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]
>   line = line.split()
>   line = line.reverse()
>   line = (' '.join(line))
>   print(line)
> 
> 

Most likely your problem is that you're using Python 2.x, and
 therefore should be using raw_input instead of input.  Of course
 the full traceback could point somewhere else, since it shows the
 line in error. 

After you fix the error that python shows you you'll still have at
 least two more. You'll need another raw_input at the end of the
 loop.

And the call to reverse will reorder the words in the line,  not
 the letters in each word.  You need a loop or equivalent.  In
 fact I'd recommend assigning the results ofbthe split to a
 different name,  since it's no longer a line, but a list of
 words.  Something like
   words = line.split ()

At that point it should be clearer that you have to loop over the
 words.

I'll point out that the outer parentheses on the join line are
 redundant but harmless.  The do nothing for the compiler and just
 confuse a human reader. 


> 
> This email is intended for the use of the named individual or entity and may contain confidential and privileged information. .......

Don't post confidential information on a public mailing list. And
 don't expect such a disclaimer to do anything but waste megabytes
 of space on computers around the world. 
> 
> 


-- 
DaveA



More information about the Tutor mailing list