在python中需要帮助(for和while循环)

时间:2016-07-26 20:43:42

标签: loops python-3.x for-loop while-loop

问)在一所大学,全日制学生的学费是每学期8,000美元。据宣布,未来5年学费每年将增加3%。编写一个带有循环的程序,该循环显示未来5年的预计学期学费金额。

这就是我得到的

学期 - 学费金额

 1
 2
 3
 4 
 5
                 8240.00
                 8480.00
                 8720.00
                 8960.00
                 9200.00

这就是我想要的

学期 - 学费金额

1                 8240.00
2                 8480.00
3                 8720.00
4                 8960.00
5                 9200.00

我想要学期旁边的学费

这是我的代码

    fees = 8000
    amount = 240
    MAX = 9200

print('Semester \t Tuition Amount')
print('----------------------------')

for x in range(1,6):
     print(x)

while fees<MAX:
     fees += amount
     print('\t\t',format(fees,'.2f'))

1 个答案:

答案 0 :(得分:0)

问题在于您打印年份,然后是费用。 Python print会在每个print语句后自动添加换行符。所以你想要的是在相同的print声明中输出年份和学费:

fees, amount = 8000, 240
print('Semester\tTuition Amount')
print('-----------------------------------------')
for x in range(1, 6):
     fees += amount
     print(x, format(fees, '.2f'), sep='\t\t')