#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Random Syntax Error?


    So I'm writing a code for an assignment which basically reads information from a file and generates some averages. I ran the code once and it worked, but then for some reason the next time I ran it I got a random syntax error and now I can't run it. Here is the code. The error references the quotation mark in red. When I remove that comma string I then get a syntax error referring to the variable following it. (Also red). What is going on?

    Code:
    # iris code
    
    file = open("iris.data", "r")
    o = open("iris-summary.txt", "w")
    
    set_count = 0
    set_sepal_length = 0
    set_sepal_width = 0
    set_petal_length = 0
    set_petal_width = 0
    vers_count = 0
    vers_sepal_length = 0
    vers_sepal_width = 0
    vers_petal_length = 0
    vers_petal_width = 0
    virg_count = 0
    virg_sepal_length = 0
    virg_sepal_width = 0
    virg_petal_length = 0
    virg_petal_width = 0
    
    
    for line in file:
        if "Iris-setosa" in line:
            c = line
            set_count += 1
            set_sepal_length = set_sepal_length + float(c[0:3])
            set_sepal_width = set_sepal_width + float(c[4:7])
            set_petal_length = set_petal_length + float(c[8:11])
            set_petal_width = set_petal_width + float(c[12:15])
        if "Iris-versicolor" in line:
            c = line
            vers_count += 1
            vers_sepal_length = vers_sepal_length + float(c[0:3])
            vers_sepal_width = vers_sepal_width + float(c[4:7])
            vers_petal_length = vers_petal_length + float(c[8:11])
            vers_petal_width = vers_petal_width + float(c[12:15])
        if "Iris-virginica" in line:
            c = line
            virg_count += 1
            virg_sepal_length = virg_sepal_length + float(c[0:3])
            virg_sepal_width = virg_sepal_width + float(c[4:7])
            virg_petal_length = virg_petal_length + float(c[8:11])
            virg_petal_width = virg_petal_width + float(c[12:15])
    
    o.writelines(vers_sepal_length/vers_count"," vers_sepal_width/vers_count"," vers_petal_length/vers_count"," vers_petal_width/vers_count", Iris-versicolor")
    o.writelines(set_sepal_length/set_count"," set_sepal_width/set_count"," set_petal_length/set_count"," set_petal_width/set_count", Iris_setosa")
    o.writelines(virg_sepal_length/virg_count"," virg_sepal_width/virg_count"," virg_petal_length/virg_count"," virg_petal_width/virg_count", Iris-virginica")
    
    file.close()
    o.close()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    You've have told an incomplete or incorrect story.

    Code:
    o.writelines(('{:6.2f}, '*4+'{}').format(vers_sepal_length/vers_count , vers_sepal_width/vers_count , vers_petal_length/vers_count , vers_petal_width/vers_count, Iris-versicolor))
    gawk will catenate strings and numbers because context determines data type. Python won't. Python requires explicit conversions.

    Comments on this post

    • Connor474 agrees
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by b49P23TIvg
    You've have told an incomplete or incorrect story.

    Code:
    o.writelines(('{:6.2f}, '*4+'{}').format(vers_sepal_length/vers_count , vers_sepal_width/vers_count , vers_petal_length/vers_count , vers_petal_width/vers_count, Iris-versicolor))
    gawk will catenate strings and numbers because context determines data type. Python won't. Python requires explicit conversions.
    Thank you! I'm not sure I fully understand what was wrong with the original code, but this works.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    Don't understand? Python doesn't understand number followed by string.

    Code:
    >>> 2 ","
      File "<stdin>", line 1
        2 ","
            ^
    SyntaxError: invalid syntax
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo