简体   繁体   中英

How do I make the while loop change its output numbers

I'm trying to make it where my code will return different years for the return statement. Input:

def rule_of_72():
    view_percent = 1
    percent = 0.01
    value = 10
    doubled = value * 2
    years = 1
    d = 0
    while(d < value):
        d = value * percent * years
        years += 1
    print("interest rate %", "Rule of 72", "Actual")
    while(view_percent <= 20):
        print("     ", view_percent, "         ",     72//view_percent, "       ", years - 1)
        view_percent += 1
        percent += 0.01

rule_of_72()

Output

its not suppose to print 100 the entire time it would print the amount of time in years that it would take for 200 to double in simple interest

I does anyone know how to fix this?

You should update years in the second loop, something like this:

while(view_percent <= 20):
    years=......
    print("     ", view_percent, "         ",     72//view_percent, "       ", years - 1)
    view_percent += 1
    percent += 0.01

Put your calculation instead of ......

Do you mean this?

while(view_percent <= 20):
    years -= 1
    print("     ", view_percent, "         ",     72//view_percent, "       ", years)
    view_percent += 1
    percent += 0.01

Output:

interest rate % Rule of 72 Actual
      1           72         100
      2           36         99
      3           24         98
      4           18         97
      5           14         96
      6           12         95
      7           10         94
      8           9         93
      9           8         92
      10           7         91
      11           6         90
      12           6         89
      13           5         88
      14           5         87
      15           4         86
      16           4         85
      17           4         84
      18           4         83
      19           3         82
      20           3         81

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM