python将你最后的答案乘以constan

2024-05-14 15:53:22 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我想用每年%的增长率来显示一定数量的年薪

print('Enter the strting salary: ', end ='')
SALARY = float(input())
print('Enter the annual % increase: ', end ='')
ANNUAL_INCREASE = float(input())

calculation1 = ANNUAL_INCREASE / 100
calculation2 = calculation1 * SALARY
calculation3 = calculation2 + SALARY



Yearloops = int(input('Enter number of years: '))

for x in range(Yearloops):
    print(x + 1, calculation3 )

这是我到目前为止的输出,输入25000作为工资,3%作为增长,5年。你知道吗

1 25750.0
2 25750.0
3 25750.0
4 25750.0
5 25750.0

我需要把最后一个答案再乘以增加的百分比。应该是这样的

1 25000.00 
2 25750.00 
3 26522.50
4 27318.17 
5 28137.72

有人能教我怎么做吗?谢谢。你知道吗


Tags: theinput数量floatendprintentersalary
3条回答

我想这会满足你的要求:

print('Enter the strting salary: ', end ='')
SALARY = float(input())
print('Enter the annual % increase: ', end ='')
ANNUAL_INCREASE = float(input())

calculation1 = ANNUAL_INCREASE / 100

Yearloops = int(input('Enter number of years: '))

newsalary = SALARY
print(1, newsalary )
for x in range(1,Yearloops):
    newsalary = newsalary*(1+calculation1)
    print(x + 1, newsalary )

我打印了第一年外的循环,因为我们不想计算增长,根据你的规格

你需要把你的计算放在for循环中,这样它每年都会发生,而不是只发生一次

salary = float(input('enter starting salary: '))
annual_increase = float(input('enter the annual % increase: '))
years = int(input('enter number of years: '))

for x in range(years):
    print(x + 1, salary)

    increase = (annual_increase/100) * salary
    salary += increase

输入25000、3%和5年输出

1 25000.0
2 25750.0
3 26522.5
4 27318.175
5 28137.72025

稍微调整一下您的代码版本:

print('Enter the strting salary: ', end ='')
SALARY = float(input())
print('Enter the annual % increase: ', end ='')
ANNUAL_INCREASE = float(input())

Yearloops = int(input('Enter number of years: '))

value = SALARY
for x in range(Yearloops):
    print('{} {:.2f}'.format(x + 1, value))
    value = value * (1 + ANNUAL_INCREASE/100)

这将生成测试用例25000、3、5的以下输出:

Enter the strting salary: 25000
Enter the annual % increase: 3
Enter number of years: 5
1 25000.00
2 25750.00
3 26522.50
4 27318.17
5 28137.72

相关问题 更多 >

    热门问题