正确使用阅读行

时间:2019-11-25 19:45:58

标签: python python-3.x

我希望能够使应用程序正确读取并计算写入文件的测试分数。但是,我当前在停止循环时收到错误消息。我正在尝试同时学习readline的概念。到目前为止,我一直尝试使用当前代码来解决问题。 当前错误消息:

Traceback (most recent call last):
  File "C:\Users\iamep\OneDrive\Desktop\TestAvgCalcEF_WIP.py", line 76, in <module>
    main()
  File "C:\Users\iamep\OneDrive\Desktop\TestAvgCalcEF_WIP.py", line 73, in main
    total, total_quiz = calculate_average(write_file())
TypeError: calculate_average() missing 1 required positional argument: 'total_quiz'

def welcomeMsg():
        print("Welcome to Test Avg Calc!!")
        print("Enter Test Scores and enter 'stop' to stop!")


def write_file():
    total = 0
    total_quiz = 0

    file = open("Scores.txt", "w")
    while True:
    #User Input and Variable to stop loop
        inpt = input("Enter score: ")
        if inpt.lower()== 'stop':
            file.close()
            break
    #Data Validation
        try:
            if int(inpt) in range(1,101):
                 total += int(inpt)
                 total_quiz += 1
                 file.write(inpt + "\n")
            else:
                print("Score too small or Big")
        except ValueError:
            print("Not a Number")



    return total, total_quiz

def calculate_average(total, total_quiz):
    scores = open("Scores.txt", "r")
    s=""
    try:
        for oneScore in scores:
            s = scores.readline()
            while s != '':
                s = int(scores.readline())
                if int(s) in range(1,101):
                    total += int(s)
                    counter += 1


        ## for oneScore in scores:   ## no readline needed

        ## s = scores.readline()
        ## while s != ''
            ## all logic
            ## s = scores.readline()


            else:
                print("Invalid data in file.")
    except ValueError:
        print("Invalid data found")
    return total, total_quiz

def displayAverage(total, total_quiz):
    average = total / total_quiz

    print('The Average score is: ', format(average, '.2f'))
    print('You have entered', total_quiz, 'scores')
#Main Function
def main():
    welcomeMsg()
    total, total_quiz = calculate_average(write_file())
    displayAverage(total, total_quiz)
#Run Main Function
main()

1 个答案:

答案 0 :(得分:3)

您的write_file()函数返回一个元组。您需要解压缩它,然后将其传递给calculate_average()函数:

x, y = write_file()
total, total_quiz = calculate_average(x, y)