[Tutor] Need all values from while loop - only receiving one

Alan Gauld alan.gauld at yahoo.co.uk
Mon Jul 2 17:51:06 EDT 2018


On 02/07/18 21:06, Daryl Heppner wrote:

> I'm trying to calculate the amount of rent per lease for the life of
> the lease, by month.  The following code provides the correct results
> except for the monthnum and billper.  


> The while loop (below) returns the first value correctly but stops
> without continuing through the rest of the True matches.

Which whiole loop? There are two.


>         for dl in deal.findall("Deal"):
>             dealid = dl.get("DealID")
>             for dts in dl.findall("DealTerms/DealTerm"):
>                 dtid = dts.get("ID")
>                 dstart = pr.parse(dts.find("CommencementDate").text)
>                 dterm = dts.find("LeaseTerm").text
>                 darea = dts.find("RentableArea").text

I'm a bit confused by this loop. You go round and round
overwriting the same values but not doing anything with them?
They eventually wind up weith the last value read.
Is that really what you want?

>             for brrent in dts.findall("BaseRents/BaseRent"):
>                 brid = brrent.get("ID")
>                 begmo = int(brrent.find("BeginIn").text)
>                 if brrent.find("Duration").text is not None:
>                     duration = int(brrent.find("Duration").text)
>                 else:
>                     duration = 0
>                 brentamt = brrent.find("Rent").text
>                 brper = brrent.find("Period").text
>                 perst = dstart + rd.relativedelta(months=begmo-1)
>                 perend = perst + rd.relativedelta(months=duration-1)
>                 billmocount = begmo

This is basically the same in that only the last value will
be stored because you overwrite the same variables each time.


>                 while billmocount < duration:
>                     monthnum = billmocount
>                     billmocount += 1

And this too is odd.
You set monthnum to billmocount
Then you increment billmocount
Then you go round and do the same until billmocount
equals duration(or somehow is slightly greater than),
at which point monthnum is one less than billmo.

So, assuming integer values are used, you could replace
the loop with:

		billmocount = duration
		monthnum = billmocount -1

If it's floats then its only slightly more complex to
work out the values directly.

>                 billmo = perst
>                 while billmo < perend:
>                     billper = billmo
>                     billmo += rd.relativedelta(months=1)

And again here.
billmo winds up equal or greater than perend
and billper has the value billmo - rd.relativedelta(months=1)

It would be more efficient to write

                 billmo = perst
                 delta = rd.relativedelta(months=1)
                 while billmo < perend:
                     billmo += delta
                 billper = billmo - delta

>                 if dealid == "706880":
>                     print(dealid, dtid, brid, begmo, dstart, dterm,
> darea, brentamt, brper, duration, perst, perend, \
>                     monthnum, billper)
> 

I'm not clear what you think you are doing but it
looks fundamentally flawed to me. I think you need
a rethink of your design.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list