Mensa: your code was very well structured and a nice example of reading user input then using it for calculation. Here's how I've modified your code:
def calc_payment(int_rate, num_pmnts, principal, freq):
''' This function will calculate the payment amount of a loan.
@ Inputs
- int_rate - The interest rate of the loan
- num_pmnts - The number of payments required
- principal - The original amount of the loan (minus down-payment)
- freq - Frequency of payments (weekly, monthly, quarterly, annually)
@ Returns
- pmnt_amt - The amount that each payment will be
'''
freq_lookup = {'weekly':52, 'monthly':12, 'quarterly':4, 'annually':1}
int_rate = float(int_rate) / 100
rr = int_rate / freq_lookup[freq]
x = (1.0 + rr) ** num_pmnts
y = rr / (x - 1.0)
pmnt_amt = (rr + y) * principal
return pmnt_amt
def main():
r = input('What is your interest rate? ')
t = input('How many payments will you make? ')
la = input('What was the amount of the loan? ')
rt = None
while rt not in ['weekly', 'monthly', 'quarterly', 'annually']:
if rt:
rt = raw_input('Sorry that was not an option, please respond with weekly, monthly, quarterly, or annually: ').lower()
else:
rt = raw_input('Do you make your payments weekly, monthly, quarterly, or annually? ').lower()
payment = calc_payment(r, t, la, rt)
print 'Your %s payment will be %.2f' % (rt, payment)
if __name__ == '__main__':
main()
raw_input('Press Enter to Exit...')
Here are the modifications I made:
1. Moved the payment calculation to a function called
calc_payment and added comments explaining what the function does, what inputs it expects and what the output means (this is something that you should get in the habit of doing, as it will help in the future and is good coding practice).
2. I removed the
r = r + .0 line and instead consolidated it with the line immediately below it using the built-in
float function (which converts the number to floating point, as does adding .0).
3. I added a dictionary lookup for the payment frequency. Each key is the word description that you ask the user for (weekly, monthly, quarterly, annually) and then the value corresponding to each key is the frequency that you were using in the calculations (52, 12, 4, 1 respectively). This allows us to use one calculation instead of doing four and then picking one of the results later.
4. Moved the user input requesting into the "main" function. Also added a statement that you'll see quite frequently when working with Python:
if __name__ == '__main__': . This is typically the only statement that is not inside of a function (aside from a main function call or some initialization stuff, which are inside this "if" block). This statement makes sure that the program is running as the 'main' script, and executes accordingly. So now, if another program were to
input <script_name> where script_name is the name of this script, that other program would not automatically run the
main function and ask the user for input. Then your 'main' program would be able to call
<script_name>.calc_payment and make use of it however you'd like.
5. Moved the part that tells the user they provided an incorrect input for frequency to where we get the input. This makes sure we don't do the calculation until the user has given us a correct input. I also used
lower to make sure the string is all lower case and a list to validate and make sure their input is kosher.
I think that's about it, but if you have any questions about my modifications, please feel free to ask for further details!