I need to write a mortgage calculator program that will find the amount of a monthly mortgage payment for a loan.

``````# Get information from user
print 'CALCULATOR FOR MORTGAGE LOANS'
loan_str = raw_input ("Amount of loan:")
loan = eval(loan_str)
years_str = raw_input("Number of years:")
years = eval(years_str)
rate_str = raw_input ("Annual percentage rate:")
rate = eval(rate_str)

# Use formula and print results
print '\$',loan, 'at', rate,'%','for', years, 'years'
print 'Monthly Payment:', '\$',loan * rate * 1 + rate ** 12  / 1 + rate ** -1``````

So far I have gotten the information from the user and converted it to numbers. What I need to do is:

- I was advised to not use "12" in the formula for months but rather total number of months power and I'm not sure how to implement that..formula is here
- Convert to months and decimals (confused with this)
- Then apply formula
- Then print results

Help is greatly appreciated!

4
Contributors
9
Replies
10
Views
7 Years
Discussion Span
Last Post by woooee

Drop all eval,where have you learn this very ugly way to convert string to integer?

``````>>> loan_str = raw_input ("Amount of loan:")
Amount of loan:20000
>>> #make integer
>>> loan = int(loan_str)
>>> loan
20000
>>> type(loan)
<type 'int'>

>>> #Or make input return an integer
>>> loan_str = int(raw_input ("Amount of loan:"))
Amount of loan:500000
>>> type(laoan_str)
<type 'int'>
>>>``````

On this site vega done a lot work over many year.
http://www.daniweb.com/code/snippet216739.html

Edited by snippsat: n/a

I learned this in school and need to follow this form -- any help?

I agree it is very unsafe to do like you did conversion by eval. The user can give any command through eval, including wiping out information from computer (by using __import__() function) Your should use float or int for conversion.

Test with known numbers producing a known output to check your calculations.

``````##--- this statement
print 'Monthly Payment:', '\$',loan * rate * 1 + rate ** 12  / 1 + rate ** -1
##
## is the same as --> there is no reason to multiply loan * rate times 1
(loan * rate * 1) + (rate ** 12)  / 1 + (rate ** -1)``````

Edited by woooee: n/a

I need to use this eval format for my assignment. I'm having trouble returning a correct answer which is below. Also below is the formula.

The number I'm returning is 2400xxx

Attachments

Also I'm a newbie to python, so explanation or anything is very helpful as I'm just learning the code! Thanks

Notice the year and month and convert the unit of rate and time accordingly. print out with:

``````print """\$%i at %i%% for %i years
Monthly payment: \$%.2f""" % (amount_of_loan,
annual_percentage,
number_of_years,
monthly_payment)``````

Edited by pyTony: n/a

Can you explain the code there? I don't quite get it...As I mentioned I'm new to all this

The usual method is to go from right to left
factor = 1+rate
factor = factor ** months

rateXfactor = rate * factor
etc.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.