Discussion:
[Tutor] line error on no. 7
l***@gmail.com
2015-07-28 20:21:19 UTC
Permalink
Hi Everyone,


I'm writing python code to read a data text file, split the file into a list of words using the split(function) and to print the results in alphabetical order.


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


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



Desired Output: ['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']


There is a line error on no. 7
What is the cause of this error?



Regards,
Hal






Sent from Surface
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Martin A. Brown
2015-07-28 23:41:20 UTC
Permalink
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/
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
l***@gmail.com
2015-07-29 02:01:50 UTC
Permalink
Thanks, I’ll need some time to review your notes






Sent from Surface





From: Martin A. Brown
Sent: ‎Tuesday‎, ‎July‎ ‎28‎, ‎2015 ‎4‎:‎41‎ ‎PM
To: ***@gmail.com
Cc: ***@python.org






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/
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/l
Loading...