1

I wrote a program in python to find compound interest (more like copied). This program was written in python 2 and I am having a problem on the last line .format(years).

I need to know what I can do with this code, and how to write it properly in Python 3. Also with the {} part in the last line. Should I change it to %s? The error says:

"AttributeError: 'NoneType' object has no attribute 'format'".

My code looks like this :

# Estimated yearly interest

print ("How many years will you be saving ? ")
years = int(input("Enter the number of years : "))

print("How much money is currently in your account ? ")
principal = float(input("Enter current amount in account : "))

print("How much money do you plan on investing monthly ? ")
monthly_invest = float(input("Monthly invest : "))

print("What do you estimate the interest of this yearly investment would be ? ")
interest = (float(input("Enter the interest in decimal numbers (10% = 0.1) : ")))

print(' ')

monthly_invest = monthly_invest * 12
final_amount = 0

for i in range(0, years ):
    if final_amount == 0:
        final_amount = principal
    final_amount = (final_amount + monthly_invest) * (1 + interest)

print("This is how much money you will have after {} years:  ").format(years) + str(final_amount)
2
  • 2
    The parentheses should be at the end instead of after the string literal. Also, using the mathematical formula would be easier Commented Jun 23, 2020 at 19:49
  • I imagine if you looked at the Python documentation you would find that it is a perfectly acceptable method to format a string. - docs.python.org/3/library/stdtypes.html#str.format. Commented Jun 23, 2020 at 19:53

5 Answers 5

5

I find it a bit of a shame that no one recommended f-strings. There are only available since Python 3.6 but they are quite powerful, easy to use and the recommended string formatting option in PEP 498 (unless I'm mistaken).

If you want to get serious about python and work with other people I really recommend reading up on best practices, in this case, f-strings.

Solution using f-strings:

print(f"This is how much money you will have after {years} years: {final_amount}")

Sign up to request clarification or add additional context in comments.

1 Comment

I am a beginner and i am still learning the basics, but thank you for your opinion. This works too.
3

Change

print("This is how much money you will have after {} years:  ").format(years) + str(final_amount)

to

print("This is how much money you will have after {} years:  ".format(years)) + str(final_amount)

format() is a method of the string class. You're using it on the print() function which is of NoneType, hence the error.

1 Comment

It works when i put the close parenthesis on the last.
1

You can do a normal string concatenation like this :

Print("This is how much money you will have after " + format(years) + " years: " +str(final_amount)

Or if you wish to keep the same format you can do this

print("This is how much money you will have after {} years: ".format(years) + str(final_amount))

Comments

1

A very basic solution would be to change the last line to:

print("This is how much money you will have after {} years:".format(years), str(round(final_amount,2)))

That will do the trick for you

Comments

1

You could also use Numpy's financial functions

For $1000 invested monthly for 10 years at annual rate of 4%:

>>> import numpy as np
>>> np.fv(.04/12, 10*12, -1000, 0)
147249.8047254786

With an initial principal of $100,000:

>>> np.fv(.04/12, 10*12, -1000, -100000)
296333.07296730485

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.