[Tutor] broken script

Martin A. Brown martin at linux-ip.net
Wed Jul 6 00:23:41 CEST 2011


Hello,

 : I am copy-typing the following pre-written program:
 : 
 : def break_words(stuff):
 :     """This function will break up words for us."""
 :     words=stuff.split(' ')
 :     return words
 :     
 : def sort_words(words):
 :     """Sorts the words."""
 :     return sorted(words)
 : 
 : def print_first_word(words):
 :     """Prints the first word after popping it off."""
 :     word=words.pop(0)
 :     print word
 : 
 : I am testing at the end of each section as I type it.  As far as line 9 it was 
 : fine - but, once I have typed up to line 14, it jibs at line 10 with the 
 : following error message:
 :     
 : lisi at Tux:~/Python/LearnPythonTheHardWay$ python ex26.py
 :   File "ex26.py", line 10
 :     def print_first_word(words)
 :                               ^
 : SyntaxError: invalid syntax
 : lisi at Tux:~/Python/LearnPythonTheHardWay$

Look at the error.  Look at the error carefully.

Look at the definitions of your other functions....

Compare.  What is different?

          do you see the colon?
                        |
                        V
def silly_function(args):

Curiously, the sample that you pasted above has the required colon 
at the end of the line which starts the function definition.

 : def print_first_word(words):
 :     """Prints the first word after popping it off."""
 :     word=words.pop(0)
 :     print word

I have heard people express frustration many times about how a 
program(ming language) or "the computer" did not understand 
something because the thing was 'missing a damned semicolon'.  

Unfortunately, these syntactical rules are quite important to our 
very fast, but not terribly intuitive friends of silicon.

 : (The caret should be under the closing bracket at the end of the 
 : line in which it is the penultimate character.)  I have deleted 
 : and re-copy-typed the end of the line repeatedly.  In despair, I 
 : deleted the end of the line and copied and pasted the end of line 
 : 6 into it.  I have checked and rechecked for incorrect 
 : white-space.  I can find nothing wrong.  I have copy-pasted it 
 : into a WP just to make the non-printing characters visible.  I 
 : can still see nothing wrong.  Why is line 6 fine and line 10 a 
 : disaster?  I can see no difference in the syntax, though there 
 : must clearly be one. :-(
 : 
 : I am at a loss as to what to do next.  (It is the next exercise 
 : that is supposed to have errors in for us to put right!!!)
 : 
 : Pointers in the right direction very gratefully received!

Try again, keep with it, and recognize that these beasties are 
awfully particular in what they accept.  And, for good reason.

Best of luck, Lisi,

-Martin

-- 
Martin A. Brown
http://linux-ip.net/


More information about the Tutor mailing list