r/pythonhelp • • Jan 12 '21

Need help with loop (beginner)

Hi! I need help fixing my coding to make my investment amounts follow a loop/show accumulated interest and so on. Here are more specific instructions with an example of what the output should look like

def futureval():
	# Read from the keyboard: amount, interest rate, periods
	# read the amount (principal)
	P: float = float(input('Please enter the amount to invest: $'))
	rate: float = float(
	    input('Please enter the interest rate per period, with no %: '))
	periods: int = int(input('Please enter the number of periods: '))
	fixed: int = int(
	    input('Please enter the amount you want to invest each period: $'))
	# Print out the initial parameters
	print('Investing $', P, 'at', rate, "% for", periods, "months at $", fixed,
	      'a month')
	# Adjust the rate from a percentage to a raw number
	rate = (rate) / 100

	# Calculate and print out the investment table
	# Print a header row
	print('Period\tInterest\tPlus interest\tIncrement\tAmount')
	# Print the starting amount
	print('Initial\t\t\t\t\t\t\t\t\t\t\t$', round(P, 2))
	# Print out the table, row by row
	for i in range(periods):
		#Add the fixed amount
		Increment = fixed
		# Calculate the interest
		interest = P * rate
		# Add the interest back into the principal
		P = interest + P
		#Calculate total amount
		Amount = P + fixed
		interest = Amount * rate

		# Print the table row
		print((i + 1), '\t\t\t$', round(interest, 2), '\t\t$', round(P, 2),
		      '\t\t$', round(Increment, 2), '\t\t$', round(Amount, 2))


if __name__ == "__main__":
	import sys
	futureval()
3 Upvotes

1 comment sorted by

1

u/MT1961 Jan 13 '21

What seems to be the problem? I ran your program, it appears to give output (didn't really check to see if it was right or not).

Actually, I'm going to guess the issue is the formatting?

Try this:

Python | Output Formatting - GeeksforGeeks