简体   繁体   中英

While Loop: how do I set (something = integer) only once during the loop

I can't figure out how to set both rate and principal to their set numbers only once

In this loop it constantly resets rate = 5 and principal = 9000 but I want the loop to continue where the principal keeps increasing to 10,000 11,000 12,000 etc.

N = 5
rate = 5
while rate <= 15: 
    principal = 9000
    principal = principal + 1000
    while principal <= 15000:
        simple = principal * (1 + (rate/100) * N)
        compound = principal * (1 + (rate/100)) ** N
        print(str(rate) + "%", principal, simple, compound)

When ran it should look like:

5% $10000 $12500.00 $12762.82
5% $11000 $13750.00 $14039.10
5% $12000 $15000.00 $15315.38
5% $13000 $16250.00 $16591.66
5% $14000 $17500.00 $17867.94
5% $15000 $18750.00 $19144.22
10% $10000 $15000.00 $16105.10
10% $11000 $16500.00 $17715.61
10% $12000 $18000.00 $19326.12
10% $13000 $19500.00 $20936.63
10% $14000 $21000.00 $22547.14
10% $15000 $22500.00 $24157.65
15% $10000 $17500.00 $20113.57
15% $11000 $19250.00 $22124.93
15% $12000 $21000.00 $24136.29
15% $13000 $22750.00 $26147.64
15% $14000 $24500.00 $28159.00
15% $15000 $26250.00 $30170.36

Initialise rate outside of while loop and increase it in first while

N = 5
rate = 0
while rate < 15:
    rate += 5
    principal = 9000
    while principal < 15000:
        principal = principal + 1000
        simple = principal * (1 + (rate/100) * N)
        compound = principal * (1 + (rate/100)) ** N
        print(str(rate) + "%", principal, simple, compound)

You need to increase the rate inside of the while loop so that it will end:

while rate <= 15:
    rate += 5

Thank you everyone for your help, I figured it out:)

N = 5
rate = 5
principal = 10000
while principal <= 15000:
    simple = principal * (1 + (rate/100) * N)
    compound = principal * (1 + (rate/100)) ** N
    print(str(rate) + "%", "$" + str(principal), "$" + str(simple), "$" + str(compound))
    principal += 1000
    if principal >= 15001:
        rate += 5
        principal = 10000
    if rate >= 20:
        principal = 15001

With a single for-loop :

  • Explicitly create a rate and principal list using range(start, stop + 1, step)
    • * unpacks range into a list , it's the same as list(range(1, 5))
  • Then use itertools.product to create the combinations or rate and principal and iterate through the generator with a for-loop
  • Print using an f-string (eg print(f'{variable}') )
from itertools import product

N = 5
rate = [*range(5, 16, 5)]
principal = [*range(10000, 16000, 1000)]

for v in product(rate, principal):
    simple = v[1] * (1 + (v[0]/100) * N)
    compound = v[1] * (1 + (v[0]/100)) ** N
    print(f'{v[0]}% ${v[1]} ${simple} ${compound:0.02f}')

Output:

5% $10000 $12500.0 $12762.82
5% $11000 $13750.0 $14039.10
5% $12000 $15000.0 $15315.38
5% $13000 $16250.0 $16591.66
5% $14000 $17500.0 $17867.94
5% $15000 $18750.0 $19144.22
10% $10000 $15000.0 $16105.10
10% $11000 $16500.0 $17715.61
10% $12000 $18000.0 $19326.12
10% $13000 $19500.0 $20936.63
10% $14000 $21000.0 $22547.14
10% $15000 $22500.0 $24157.65
15% $10000 $17500.0 $20113.57
15% $11000 $19250.0 $22124.93
15% $12000 $21000.0 $24136.29
15% $13000 $22750.0 $26147.64
15% $14000 $24500.0 $28159.00
15% $15000 $26250.0 $30170.36

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