我目前刚开始编写Python代码,并且正在努力处理一小段代码,我似乎无法弄清楚。我的代码基本上接受起薪,并输出在用户输入多少年的过程中增加的工资,并从输出中生成一个表。除了一个问题外,我的代码中的所有内容都是正确的。我需要以用户输入的起薪开始表格,而不是以您在第2年的收入开始表格。
percent = float(input("Enter the annual percent increase: "))
years = int(input("Enter the number of years: "))
percent = percent / 100
print("%-7s%10s" % ("Year","Salary"))
for i in range(1,years):
starting += (starting * percent)
print("%2d%17.2f" % (i, starting))
尝试交换打印的顺序和加法:
for i in range(1,years):
print("%2d%17.2f" % (i, starting))
starting += (starting * percent)
试试这个
starting = 1000 # sample starting
percent = float(input("Enter the annual percent increase: "))
years = int(input("Enter the number of years: "))
percent /= 100
print("%-7s%10s" % ("Year", "Salary"))
for i in range(years):
print("%2d%17.2f" % (i, starting))
starting += (starting * percent)
会给
Year Salary
0 1000.00
1 1030.00
2 1060.90