[Tutor] line error on no. 7

Martin A. Brown martin at linux-ip.net
Wed Jul 29 01:41:20 CEST 2015


Hello again,

> The raw python code is located at http://tinyurl.com/oua9uqx

It is not very long, so you can post inline (I pasted it below).  If you have
a longer piece of code, then, yes, a pastebin is a good choice.  (Also, if in
the future, you have a more complex piece of code, try to simplify your
posting to just the part that is giving you trouble.)  But, your question is
clear here.

   fname = raw_input("Enter file name: ")
   fh = open(fname)
   lst = list()
   for line in fh:
       if fh == list: continue
       list.split()
       list.append
       sorted("fh")
   print line.rstrip()

You have confused your names in line 7.  list.split()

There are a few lessons to learn here.

   * Are you operating on the variable you intend to be operating on?
     No, in this case.  You wanted to do something like 'lst.split()'

   * Did you want to throw away the result of the call to .split()?
     I'm guessing the answer is 'No.'  So, you'll need another
     variable to hold the value from lst.split().  Line 7 should
     become:  lst.extend(line.split())

Additional comments:

   * Your line 6 performs a test to see if fh (file object referring
     to the stanza) equals the builtin called 'list'.  That doesn't
     make sense.  Try typing list and list() at an interactive
     prompt, and you may see that it doesn't make sense to compare
     those things.

       >>> list
       <type 'list'>
       >>> list()
       []

     The first tells you what 'list' is.  The second calls 'list()',
     which returns you, well... a new Python list object.

   * Your line 8 is a NOOP (no-operation).  In that line, you are
     simply referring to a method on the builtin list type.  Use the
     interactive interpreter to see what I mean:

       <method 'append' of 'list' objects>
       >>>

   * Your line 9 doesn't make much sense to me.  It may no longer be
     a syntax error, but it isn't doing what you think it's doing.
     Try it out in the interactive interpreter to see what it's
     doing:

       >>> sorted("fh")
       ['f', 'h']

OK, so now, let me make a few suggestions.  Others may have 
additional comments, but I'd be interested in seeing how you 
adjust your program after working through the above.

Thus, I suggest removing most of the loop internals and rewriting. 
Everything else is a good start.  I'll suggest a possible ending, 
too.

   fname = raw_input("Enter file name: ")
   fh = open(fname)
   lst = list()
   for line in fh:
       # Code is good until this point.
       # Here, you want to find the words and add them to the list
       # called 'lst'.
   # -- now, outside the loop, I would suggest printing the list
   print lst

Once you can run your program and get something other than an empty 
list, you know you have made progress.

Good luck with your iambic pentameter,

-Martin

> The sample data is located at
> http://tinyurl.com/odt9nhe

Also, I'm including your short data sample:

   But soft what light through yonder window breaks
   It is the east and Juliet is the sun
   Arise fair sun and kill the envious moon
   Who is already sick and pale with grief


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


More information about the Tutor mailing list