Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop Homework Help
#1
Hey guys I'm currently in an Intro. to Python course and was hoping to get some help with this assignment I'm working on. So, this is the question:

(Financial application: compare loans with various interest rates) Write a program that lets the user enter the loan amount and loan period in number of years and displays the monthly and total payments for each interest rate starting from 5% to 8%, with an increment of 1/8. Here is a sample run:



This is what it should look like when it displays:

Loan Amount: 10000
Number of Years: 5
Interest Rate Monthly Payment Total Payment

5.000% 188.71 11322.74
5.125% 189.28 11357.13
5.250% 189.85 11391.59
...
7.875% 202.17 12129.97
8.000% 202.76 12165.83


^The numbers above should line up with the columns stated above them as well ^

I am just completely lost with loops and how to even set up the problem, but this is the code I have so far:

loanValue = eval(input("Loan Amount: "))
loanYears = eval(input("Number of Years: "))
print("Interest Rate    Monthly Payment    Total Payment")
Reply
#2
which version of python are you using?
Are you allowed to use f-strings?
Reply
#3
(Oct-15-2018, 08:05 PM)Larz60+ Wrote: which version of python are you using?
Are you allowed to use f-strings?

I'm using version 3.7.0 and yes we can use f-strings!
Reply
#4
How are you calculating the interest? I used A = P ( 1+r ) ^ t and got a different amount.
Reply
#5
All right, break the problem down into smaller parts. You'll need to:

1. Write a function (or just a line of code) that will calculate the interest.
2. Loop over that function to perform the calculation with each interest rate.
3. Provide an output for the value of the interest rate, monthly payment, and total payment.

You mentioned that you're stuck on #2. Is there anything in particular about for loops that is... throwing you for a loop? ...it had to be done.

What have you learned about for loops or loops in general?
Reply
#6
Using f-string, you can space columns as follows:
Example:
# f'result: {value:{width}.{precision}}'
# use '<' for left justified in field (see example below)
column_width = 20


def print_heading(heading):
    for value in heading:
        print(f'{value:{column_width}} ', end = '')
    print()

def main():
    print_heading(['Interest Rate', 'Monthly Payment', 'Total Payment'])
    # try floating point with '%'
    percent = '5.125%'
    print(f'{percent:{column_width}}')

if __name__ == '__main__':
    main()
result:
Output:
Interest Rate Monthly Payment Total Payment 5.125%
Good article on formatting here: https://medium.com/@NirantK/best-of-pyth...1f9154983e
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help on sequence and loop homework vzsnv 3 2,206 Nov-04-2020, 05:02 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020