繁体   English   中英

正确利用readline

[英]Properly Utilize readline

我希望能够让应用程序正确读取和计算写入文件的测试分数。 但是,我目前在停止循环时收到错误消息。 我正在尝试同时学习 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()

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM