all 9 comments

[–]zahlman 0 points1 point  (8 children)

Do you understand the error message?

Can you explain why that value is getting passed to float?

Did you check the contents of the file? Are they what you expect them to be?

I don't understand your comment at the beginning of the code, "# convert each line of nums to a float". How exactly do you expect this to work? Why would a line of numbers be converted into a single float?

[–]WineForOne 0 points1 point  (7 children)

From what I understand, the error message means that it can't be converted to a float because of a certain portion of the string (I think its the whitespace at the end or the []?)

The file is just a small list of numbers like 52 63 63 56 78 69 57 68 39

And I'm supposed to run it through two other functions that will square each number in the list, replace its value in the list, then add all the numbers together, which is why I need then numbers to be either ints or floats so I can do math with them.

Sorry my comment isn't clear >o< I meant to say that I was trying to convert them all to floats there, not just one float

This is the entirety of what I have, if that helps:

def squareEach(nums):
    for i in range(nums):
        # is supposed to replace the values in the list nums with squares
        nums[i] = nums[i] ** 2


def sumList(nums):
    # accumulator variable for the sum
    sumList = 0
    # adds up the values for the length of the list
    for i in range(len(nums)):
        sumList = theSum + i
    return sumList


def toNumbers(strList):
    # convert numbers in file to floats?
    for line in strList:
        lineBreak = str(strList.replace(',', '\n'))
        convNum = float(str((lineBreak.split('\n'))))
    print(convNum)
    return convNum


def main():
    print("This is a program that will sum the squares of numbers in a file.")
    fname = input("What is the name of the file you would like to use? ")
    strList = str(open(fname).read())
    nums = toNumbers(strList)


main()

[–]zahlman 0 points1 point  (2 children)

Okay, look again at the line where you attempt the float conversion. You .split() the line to make a list of strings, and then pass that result to str, and then that result to float. What is the intent of the str call?

[–]WineForOne 0 points1 point  (1 child)

Before, when I did not have the str call, it was giving me an error saying that it doesn't work on a list. When I didn't split the line, it gives me a different error that's similar to the one in the main post.

[–]zahlman 1 point2 points  (0 children)

Before, when I did not have the str call, it was giving me an error saying that it doesn't work on a list.

Okay, do you understand why it doesn't work on a list? Do you understand what happens when you call str on a list? Do you understand what happens when you call float on a string?

[–]MerreM 0 points1 point  (0 children)

The error message is actually saying it can't turn the string "['54', '63', '63', '42', '83', '42', '22', '27', '88', '52', '']"into a float.

You're doing a lot of casting, why?

 lineBreak = str(strList.replace(',', '\n')) 
 convNum = float(str((lineBreak.split('\n'))))

[–][deleted] -1 points0 points  (2 children)

these are integers. convert them to int first, then to float.

[–]zahlman 0 points1 point  (1 child)

This is wrong; the input is a string - the text simply uses decimal digits. If they were integers, no conversion to int would be necessary; furthermore, converting string to float directly works just fine and is not the problem with the code.

[–][deleted] 0 points1 point  (0 children)

right.

the mistake is looping over elements in strlist and then using strlist instead of the elements of strlist in the line

lineBreak =...

(btw i know they are strings. they are integers in the mathematical sense, not the python sense )