简体   繁体   中英

Properly Utilize readline

I would like to be able to get the application to properly read and calculate the test scores written to the file. However, I currently receive error messages when stoping the loop. I'm trying to learn the concepts for readline at the same time. The current code is what I've attempted so far to fix the issues. Current Error Messages:

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()

Your write_file() function returns a tuple. You need to unpack it, to pass it to calculate_average() function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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