(Very Newbie) Problems defining a variable
febaen at gmail.com
febaen at gmail.com
Fri Dec 12 12:42:55 CET 2008
#!/usr/bin/python
#Py3k, UTF-8
bank = int(input("How much money is in your account?\n>>"))
target = int(input("How much money would you like to earn each year?
\n>>"))
interest = 0
i = 0
while interest < target:
#determine the interest rate to use
if bank >= 9999:
rate = 0.006
elif bank >= 10000 and bank <= 24999:
rate = 0.0085
elif bank >= 25000 and bank <= 49999:
rate = 0.0124
elif bank >= 50000 and bank <= 99999:
rate = 0.0149
elif bank >= 100000:
rate = 0.0173
#Now that we know what interest rate to use, apply it...
lastbank = bank #To calculate interest...
bank += (bank * rate) #Update earnings...
interest = bank - lastbank #And figure out how much interest is made!
i += 1 #So we can see which year a calculation represents
print("Year %s, at %s rate: %s paid, %s in bank." % (i, rate,
interest, bank))
I wrote this expanding off an 'interest' exercise in a tutorial, which
was fairly simple (assume %1, calculate for ten years). It's intended
to take how much the user has in the bank and determine how long it
will be until it generates a certain amount in interest each year. The
problem is that rates are not solid, and increase at certain points. I
put the rates from the basic account option at my bank in as an
example.
I'm pretty certain that that is also the problem in the code. I'm
pretty sure it's a problem with the 'if' statements', and it looks
like it's one of those mistakes that's so simple you look back on it
and laugh at yourself. If you put in a bank number <= 9999, it fails,
saying "NameError: name 'rate' is not defined". If you put in one
higher, it runs correctly, but thinks that the rate is 0.006
I tried def'ing a function for it, which didn't work any better. I'm
having a hard time figuring out exactly why it is those if statements
are wrong.
More information about the Python-list
mailing list