share
Stack OverflowPython : Float object no iterable
[-3] [2] Serkan Kılınç
[2017-12-01 06:55:10]
[ python recursion ]
[ https://stackoverflow.com/questions/47588114/python-float-object-no-iterable ]

I have to calculate a equation that have a recursion. But if I am execute the code i get the failure that the Float object is not iterable

my code is:

def v(t, c):
    result = []
    if t == 0 or c == 0:
        return 0
    q = v(t - 1, c) - v(t - 1, c - 1)

    return max((0.2*(400-q)), (0.6*(400-q)), (1*(1200-q)), (0.85*(1115-q)), (0.87*(1127-q))) + v(t-1,c)

x = v(2, 1) print(x)

What can I do to get the result? Thank you

(2) max takes an iterable, you pass it a float. The error message is quite to the point. What do you want the maximum of there when you pass a single value to the function? - user2390182
can you post error message here ?/ - Akshay Tilekar
Max should be passed a tuple. Float given - Dhruv Aggarwal
[0] [2017-12-01 07:09:04] zhang lei

You are using max(), but args in your function is a tuple (0.85 * (1115 - q)) + (0.87 * (1127 - q)), this is not right


in reality my code is: return max((0.2*(400-q)), (0.6*(400-q)), (1*(1200-q)), (0.85*(1115-q)), (0.87*(1127-q))) + v(t-1,c) i delete a part of the code to make it easier to understand - Serkan Kılınç
1
[0] [2017-12-01 07:20:07] Akshay Tilekar

max() needs to be passed more than one argument and in your case only one argument is there which is causing the error. Try removing max or add one more results to your max() function to make it behave correctly.

def v(t, c):
    result = []
    if t == 0 or c == 0:
        return 0
    q = v(t - 1, c) - v(t - 1, c - 1)
    return ((0.85 * (1115 - q)) + (0.87 * (1127 - q))) + v(t - 1, c)

x = v(2, 1)
print(x)

in reality my code is: return max((0.2*(400-q)), (0.6*(400-q)), (1*(1200-q)), (0.85*(1115-q)), (0.87*(1127-q))) + v(t-1,c) i delete a part of the code to make it easier to understand - Serkan Kılınç
it is running fine then ? i have checked it by running online and it is giving 1200 as an answer !! - Akshay Tilekar
@SerkanKılınç please accept and upvote answer if it solved your problem - Akshay Tilekar
2