## marcom10

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!

## snippsat 661

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

## marcom10

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

## pyTony 888

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.

## woooee 814

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

## marcom10

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

## marcom10

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

## pyTony 888

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

## marcom10

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

## woooee 814

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

rateXfactor = rate * factor
etc.