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!

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.
So here is a some help you can look at.
http://www.daniweb.com/code/snippet216739.html

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

Your link is not working. Please post directly to Daniweb. You can attach image in Advanced Editor -> Manage Attachments.

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)

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

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)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.