Call Now
Quick Inquiry

Python Error - Float object is not iterable error in python ?

Python Error - Float object is not iterable error in python ?

The error comes from passing a single float to sum, which expects an iterable (like a list).

I can't quite tell what your original intent is, since you're mixing both procedural and functional styles. I'd recommend that you either incrementally sum ZCB yourself, using the += operator, or else, since all your arguments are the same length, use numpy to do vector math on them.

The reason I hesitate to rewrite your code in a functional style is because you're only using sum() on the first part of your formula, and I'm not sure whether you meant to sum the entire thing. Leaving your formula alone, the second part will iterate and produce multiple values for ZCB:

 import numpy as np
sum(CP/(1+np.array(ytm))**np.array(iterations)) + (FV+CP)/((1+np.array(ytm))**np.array(iterations))
array([162407.65073366, 158404.21266146, 154482.50164677, 150656.78642701,
       146896.169642  , 143206.13145474, 139553.99871209, 135986.89550577,
       132528.65334532, 129123.4633484 , 125775.53903412, 122512.19342737,
       119406.9801568 , 116365.77283386, 113461.4536341 , 110755.24145864,
       108160.10924376, 105704.56954205, 103416.55213126, 101263.70203759,
        99213.23619558,  97276.48018957,  95493.00572611,  93807.47916263,
        92215.90751991,  90714.30147789,  89291.81257265,  87951.62415916,
        86689.91102085,  85496.36006465])

33 0

Write a Comments

* Be the first to Make Comment

GoodFirms Badge
GoodFirms Badge

Fix Your Meeting With Our SEO Consultants in India To Grow Your Business Online