justin
justin

Reputation: 563

while loop not executing, is integer value being tested too high?

Summary: while (1000000000 <= 0) #1 billion is being tested. Is this too high of a value for an integer to be tested in Python or is my code(below) not executing the while loop for some other reasons?

I am trying to determine how long it would take to spend a billion dollars by spending $100/second while the money is also earning 1% interest per year:

currentAmount = 1000000000 # 1 billion

MONEY_SPENT_PER_SECOND = 100

days = 1 #up to 365

INTEREST_RATE_PER_DAY = .01 / 365

SECONDS_IN_DAY = 86400

MONEY_SPENT_PER_DAY = SECONDS_IN_DAY * 100 # $8,640,000

interestEarnedToday = 0

while(currentAmount <= 0):
    print("In loop") #test to see if code is in loop, but this does not get printed
    interestEarnedToday = INTEREST_RATE_PER_DAY * currentAmount
    currentAmount = currentAmount + interestEarnedToday
    currentAmount = currentAmount - MONEY_SPENT_PER_DAY
    days = days + 1

But the while loop is never entered and I am not sure why? As print(currentAmount) outputs 1000000000 so I feel like this indicates that python can handle integers this large. Thanks for any help.

Upvotes: 0

Views: 52

Answers (1)

Mikhail Burshteyn
Mikhail Burshteyn

Reputation: 5002

Your loop condition seems to be inverted, so it is false during the first check and this is why the loop body never runs.

You should put currentAmount >= 0 in your loop condition.

Speaking of integer size in Python, they can be arbitrarily large, so you should not worry about them being out of some range (like 32-bit or 64-bit in other languages).

Upvotes: 3

Related Questions