Quadratic equation

Benjamin Kaplan benjamin.kaplan at case.edu
Fri Mar 13 17:38:13 EDT 2009


On Fri, Mar 13, 2009 at 5:01 PM, Daniel Sidorowicz <dsi1289 at gmail.com>wrote:

> For instructions open file named README
> Enter control-C to quit
> Traceback (most recent call last):
>   File "main.py", line 57, in <module>
>     main()
>   File "main.py", line 16, in main
>     S = L.split()
> AttributeError: 'list' object has no attribute 'split'
>
> So there's my whole trace back error.
>
> I somewhat understand what your are talking about,
> expect when I do readline it will only run the function with the first line
> of my file abc.txt
> which includes all the variables i need to pass in.
>
> My file abc.txt looks like;
> 0 0 0
> 0 0 1
> 1 0 1
> that's just an example.
>
> Now I need it to save each line as a a string so I can run with in my
> program.
>
> Thanks for being helpful.
>

You're going to want to loop through L. Every element in L is a string, and
you can call split on each individual element. Since this is a homework
assignment, I don't want to give too much away. Try looking at the docs if
you don't understand how to use lists.

>
> On Fri, Mar 13, 2009 at 3:17 PM, Benjamin Kaplan <benjamin.kaplan at case.edu
> > wrote:
>
>>
>>
>> On Fri, Mar 13, 2009 at 4:06 PM, Daniel Sidorowicz <dsi1289 at gmail.com>wrote:
>>
>>> Hello I need some hello I need some help with a programingproject, I'm
>>> fairly new to programming so I do find it slightly confusing.
>>>
>>> Here is my code for my main function which
>>> passes my variable from a text file,
>>> however when i run it I get the error code:
>>>
>>> import math
>>> import quadroot
>>>
>>> def main():
>>>         print "\n--------------------------------------------\n"
>>>         print "Daniel Sidorowicz \n", "325 \n", "MCS 260, Spring 2009
>>> \n", "mp1id325"
>>>         print "For instructions open file named README"
>>>         print "Enter control-C to quit"
>>>         infile = open("abc.txt",'r')
>>>         L = infile.readlines()
>>>         S = L.split()
>>>         print S
>>>         infile.close()
>>>         outfile = open("output.txt",'a')
>>>         eq = "ax^2.0 + bx + c = 0\n"
>>>
>>>         for line in L:
>>>
>>>                 coef= a,b,c = (float(S[0]),float(S[1]),float(S[2]))
>>>                 print coef
>>>                 print "\n--------------------------------------\n"
>>>                 d = b**2.0 - 4.0*a*c
>>>
>>>                 if a != 0:
>>>                     if d == 0.0 :
>>>                      outfile.write(eq)
>>>                      outfile.write(str(quadroot.root1(a,b,c,d)))
>>>                     elif d > 0.0 :
>>>                      outfile.write(eq)
>>>                      outfile.write(str(quadroot.root2(a,b,c,d)))
>>>                     elif d < 0.0 :
>>>                      outfile.write(eq)
>>>                      outfile.write(str(quadroot.root3(a,b,c,d)))
>>>                     else :
>>>                       "Never get here!"
>>>                 elif a ==  0 :
>>>                    if b != 0:
>>>                      outfile.write(eq)
>>>                      outfile.write(str(quadroot.root5(b,c)))
>>>                    else:
>>>                       if c == 0:
>>>                        outfile.write(eq)
>>>                        outfile.write(str(quadroot.root6()))
>>>                       else:
>>>                          outfile.write(eq)
>>>                          outfile.write(str(quadroot.root4(a,b,c,d)))
>>>                 else:
>>>                   "Never get here!"
>>>
>>>         outfile.close()
>>>
>>> main()
>>>
>>> AttributeError: 'list' object has no attribute 'split'
>>>
>>> Can someone please explain/clarify this issue to me thanks!
>>>
>>
>> First of all, that's not the entire traceback. The entire traceback should
>> include a line number that corresponds to the line "S=L.split()". Look at
>> the error message- it says you are trying to access the split attribute of a
>> list object, which doesn't exist. So, L is a list and it doesn't have a
>> split method. Here is the documentation for file.readlines :
>>
>> readlines(...)
>>     readlines([size]) -> list of strings, each a line from the file.
>>
>>     Call readline() repeatedly and return a list of the lines so read.
>>     The optional size argument, if given, is an approximate bound on the
>>     total number of bytes in the lines returned.
>>
>> So by calling readlines, you already have a list where each element is a
>> single line in the file. What you want to do from there depends on the
>> structure of the file. Look at the documentation for list and str if you
>> can't figure out what to do. Also, use the interactive prompt to mess around
>> with the types before you try writing the program.
>>
>>
>> http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange
>>
>>
>>
>>>
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090313/7bd1f1de/attachment-0001.html>


More information about the Python-list mailing list