-4

Trying to put float() everywhere but i still get the same message.

def getAmountOfCarbon(volume):
    carbon = 1.8 + 2 * math.log(volume)
    return carbon

    carbon = []
    for listitem in lists:
        carbonlist = getAmountOfCarbon(volume)
        carbon.append(carbonlist)

My lists variable is a function:

lists = readCSVfile(str1)

print carbon
TypeError: a float is required

Where should i put my float()?

edit: the volume comes from:

def getVolume(width, height, length):
    volume = (width) * (height) * (length) 
    return volume

edit: I call volume here:

volume = []
for listitem in lists:
    volumelist = getVolume(listitem[2], listitem[3], listitem[4])
    volume.append(volumelist)

Edit: Solved it in a different way:

def createAnalyseList(lists):
    analyselist = []
    for item in lists:
        height = getHeightType(item[4])
        carbon = getAmountOfCarbon(getVolume(item[2],item[3],item[4]))
        analyselist.append([item[0], item[1], height, carbon])
    print analyselist
    return analyselist
analyselist = createAnalyseList(lists)
  • 6
    show your `lists` variable, please – Eugene Soldatov Oct 19 '15 at 14:12
  • When and how does `listitem` magically become `volume`? – tobias_k Oct 19 '15 at 14:14
  • 5
    This question is missing some things. First, your indentation is wrong. Then we don't know what getAmountOfCarbon(). You're using the name carbon twice for different things - this is somewhat confusing. In the end we have no idea what throws the error. – Hubert Grzeskowiak Oct 19 '15 at 14:14
  • Is `lists` a list of lists` If so, try `carbonlist = [getAmountOfCarbon(volume) for volume in listitem]` – tobias_k Oct 19 '15 at 14:17
  • Yes, lists is a list of list @tobias_k – Erik Angerfist Nyström Oct 19 '15 at 14:19
  • That's the error you would get if you passed (for example) a `str` object to `math.log`. How is `lists` being populated, and where do you actually call `getVolume`? – chepner Oct 19 '15 at 14:19
  • You `improved formatting` but do you really know what you are doing? Is it really *your* code? – Wolf Oct 19 '15 at 14:21
  • 2
    It is my code, but i am big newbie trying to do a school assignment @wolf – Erik Angerfist Nyström Oct 19 '15 at 14:23
  • 1
    I see. I think you erroneously indented the lines after `return carbon` in the first code snippet. – Wolf Oct 19 '15 at 14:25
  • What does your `readCSVfile` function look like? It appears that you are not converting the data you read from strings into floats. – PM 2Ring Oct 19 '15 at 14:31
  • StackOverflow is a good place to learn from others. Don't give up! – Below the Radar Oct 19 '15 at 14:34
  • BTW, your indentation in several of your code blocks is still incorrect. Indentation is **very** important in Python: the wrong indentation will cause your code to crash, or even worse, it will still run, but it will do something different to what you think it's doing. – PM 2Ring Oct 19 '15 at 14:34
  • In there _any_ difference in the two big blocks in your `readCSVfile` function _except_ the file name? – tobias_k Oct 19 '15 at 14:44
  • 1
    I read that it is against the rules to thank people, but thanks for trying to help me with my messy code :) I have edited in my readCSVfile function, and the try: part is supposed to convert the numbers to ints and floats – Erik Angerfist Nyström Oct 19 '15 at 14:45
  • @tobias_k no there is no difference except the files – Erik Angerfist Nyström Oct 19 '15 at 14:47
  • I don't see where you have passed the value for Volume in the code you shared. If you have not, this might be causing the error. –  Oct 19 '15 at 14:16

2 Answers2

0

Making a few changes, assuming your lists variable is a list of ints/floats I've executed your example successfully (also assumed that listitem and volumeare the same):

import math

def getAmountOfCarbon(volume):
    carbon = 1.8 + 2 * math.log(volume)
    return carbon

carbon = []

lists = [1.0, 5, 47, 89]

for listitem in lists:
    carbonlist = getAmountOfCarbon(listitem)
    carbon.append(carbonlist)

print carbon
jlnabais
  • 829
  • 1
  • 6
  • 18
  • 1
    Good guess. Let's hope that the asker cares for making the question match the answer. ;-) – Wolf Oct 19 '15 at 14:19
  • This gives the same Error – Erik Angerfist Nyström Oct 19 '15 at 14:26
  • I've tried to run this version in python 2.7 and the modified version for python 3.4 and both output `[1.8, 5.018875824868201, 9.500295203420118, 10.77727273946428]`. – jlnabais Oct 19 '15 at 14:29
  • The OP is reading number strings from a CSV but it looks like he's not converting those strings to `float`s or `int`s. – PM 2Ring Oct 19 '15 at 14:29
  • Haven't noticed he edited the question, guess you're right @PM2Ring, if that's the issue Erik can cast each value to float ([more here](http://stackoverflow.com/questions/379906/parse-string-to-float-or-int)) – jlnabais Oct 19 '15 at 14:31
0

Okay, I think now I see what's the problem. It seems you are doing this in your code:

lists = readCSVfile(str1)

volume = []
for listitem in lists:
    volumelist = getVolume(listitem[2], listitem[3], listitem[4])
    volume.append(volumelist)

carbon = []
for listitem in lists:
    carbonlist = getAmountOfCarbon(volume)
    carbon.append(carbonlist)

Here, volume in the second loop is still the entire list of volumes created in the first loop, and you are trying to convert that list to one "carbon" value (thus the TypeError) and append it to the carbon list for each value in the original lists! That does not seem to make much sense.

Instead, try this:

carbon = []
for listitem in volume:
    carbonlist = getAmountOfCarbon(listitem)
    carbon.append(carbonlist)

Or using map (Python 2 only):

carbon = map(getAmountOfCarbon, volume)

Or as a list comprehension:

carbon = [getAmountOfCarbon(x) for x in volume]

Or all at once (not tested):

carbon = [getAmountOfCarbon(volume(*lst[2:5])) for lst in lists]

Also, note that the code in your readCSVfile function is all duplicate. You can just do like this:

def readCSVfile(str1):
    with open(str1 + '.csv', 'r+') as f:
        ....
tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • it worked fine until i added another for loop for another calculation, now i get the same message again. The problem must be elswhere – Erik Angerfist Nyström Oct 19 '15 at 15:12
  • @ErikAngerfistNyström To be honest, I'd rather think that you made the same mistake again. – tobias_k Oct 19 '15 at 15:23
  • I got some help from an student at my university who guided me to a completely different approach on this problem. I will put it as an edit in my post. Thank you for the help, it was really nice :) @tobias_k – Erik Angerfist Nyström Oct 19 '15 at 18:06