Python:循环后打印运行总计和值

时间:2019-03-16 03:01:49

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

我在打印运行总计和最后的“ averageRaise”值时遇到麻烦。看起来当从输入文件中读取新行时,它将在输出末尾打印我要打印的四个语句。下面是文本文件,程序代码和一部分输出,用于查看模式:

程序代码:

def main():

inFile = open('Program7.txt', 'r')

memberNumber = 1
totalRaise = 0
payTotal = 0
newPayTotal = 0


lineRead = inFile.readline()
while lineRead != '':
    words = lineRead.split()
    for word in words:
        pay = float(word)

        if pay > float(60000):
            newPay = pay * 1.04
        elif pay > float(50000):
            newPay = pay * 1.07
        else:
            newPay = pay * 1.055


        payRaise = newPay - pay
        print('Pay raise for faculty member #' + str(memberNumber) + ': ' + '$' + str(format(payRaise, ',.2f')))

        memberNumber += 1
        totalRaise += float(payRaise)
        payTotal += pay
        newPayTotal += newPay
        averageRaise = totalRaise / (memberNumber - 1)

    print('The total amount of raises is: $' + str(format(totalRaise, ',.2f')))
    print('The average of all the raises is: $' + str(format(averageRaise, ',.2f')))
    print('Total amount of faculty pay without raise: $' + str(format(payTotal, ',.2f')))
    print('Total amount of faculty pay with raise: $' + str(format(newPayTotal, ',.2f')))


    lineRead = inFile.readline()

# Close the file.
inFile.close()

# Call the main function.
main()

输入文件:

52500.00       64029.50      56000.00      50001.00
65500.00       42800.00      45000.50      68900.00
60000.00       59999.94      54120.25      64100.00
44000.50       80100.20      90000.00      41000.00
60500.50       72000.00      50000.01      50000.00
80001.75       60001.00

输出的第一部分:

Pay raise for faculty member #1: $3,675.00
Pay raise for faculty member #2: $2,561.18
Pay raise for faculty member #3: $3,920.00
Pay raise for faculty member #4: $3,500.07
The total amount of raises is: $13,656.25
The average of all the raises is: $3,414.06
Total amount of faculty pay without raise: $222,530.50
Total amount of faculty pay with raise: $236,186.75
Pay raise for faculty member #5: $2,620.00
Pay raise for faculty member #6: $2,354.00
Pay raise for faculty member #7: $2,475.03
Pay raise for faculty member #8: $2,756.00
The total amount of raises is: $23,861.28
The average of all the raises is: $2,982.66
Total amount of faculty pay without raise: $444,731.00
Total amount of faculty pay with raise: $468,592.28
Pay raise for faculty member #9: $4,200.00
Pay raise for faculty member #10: $4,200.00
Pay raise for faculty member #11: $3,788.42
Pay raise for faculty member #12: $2,564.00
The total amount of raises is: $38,613.69
The average of all the raises is: $3,217.81
Total amount of faculty pay without raise: $682,951.19
Total amount of faculty pay with raise: $721,564.88

1 个答案:

答案 0 :(得分:0)

查看最后4条打印语句的缩进级别。它们每行执行一次,因为它们位于每行执行一次的循环中。将它们移到循环的外部,您应该得到想要的东西:

lineRead = inFile.readline()
# start of the loops which executes once per line
while lineRead != '': 
    words = lineRead.split()  

    # rest of your code here . . .

    lineRead = inFile.readline() 

print('The total amount of raises is: $' + str(format(totalRaise, ',.2f'))) 
print('The average of all the raises is: $' + str(format(averageRaise, ',.2f'))) 
print('Total amount of faculty pay without raise: $' + str(format(payTotal, ',.2f'))) 
print('Total amount of faculty pay with raise: $' + str(format(newPayTotal, ',.2f')))