Quadratic equation

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


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/28a39898/attachment.html>


More information about the Python-list mailing list