0

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.

justin
  • 553
  • 1
  • 7
  • 18
  • 3
    Did you mean `while currentAmount >= 0`? At the moment the loop condition is never true from the start so doesn't execute – N Chauhan Aug 26 '18 at 22:15
  • Believe it or not, other people already asked questions (and wrote documentation) about such things - https://stackoverflow.com/a/9860611/2864740 Honor their time. – user2864740 Aug 26 '18 at 22:18

1 Answers1

3

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).

Mikhail Burshteyn
  • 4,762
  • 14
  • 27
  • I looked at that multiple times and thought "no thats correct." I don't know where my minds at today but thanks, I'm not sure how long it would have been until I realized that lol – justin Aug 26 '18 at 22:22
  • yes I have to wait a few more minutes before it will allow me but will do – justin Aug 26 '18 at 22:24