简体   繁体   English

将文本和整数写入文件,然后从文件中读取以计算平均成绩

[英]Writing text and intergers to a file, then reading from the file to calculate grade averages

This is what I am trying to display.这就是我想要展示的。 These are the by products of 2 functions.这些是两个功能的副产品。 The first paragraph is the create() function which creates the "grades.txt" file.第一段是 create() function,它创建“grades.txt”文件。 I think I got down pretty easily.我想我很容易就下来了。

Secondly, the next paragraph is from the "Retrieve()" function reading from the "grades.txt" file and displaying the class name and GPA then calculating average for those classes and displaying them.其次,下一段来自“Retrieve()”function,从“grades.txt”文件中读取并显示 class 名称和 GPA,然后计算这些类的平均值并显示它们。 I feel like Im right at the cusp but im stuck as to where to go from here because im plagued with "STRG" errors.我觉得我正处于风口浪尖,但我坚持从这里到 go 的位置,因为我受到“STRG”错误的困扰。

 > def main (): > create() > retrieve() > > def create(): > outfile = open('grades.txt', 'w') > count_files = 0 > > #Gather class name or press 'Enter' to quit > class_or_exit = input('Enter course name or Enter to quit: ') > > #Create While loop if 'Enter' is pressed exit and close file > while class_or_exit:="": > count_files += 1 > grade =(input('Enter grade (interger) achieved: ')) > class_or_exit = input('Enter course name or Enter to quit. ') > > > > #Write info to the file > outfile.write(str(class_or_exit) + '\n') > outfile.write(str(grade) + '\n') > > #Close file > outfile,close() > print('File was created and closed') > return grade: class_or_exit > > def retrieve(): > print('Here is your GPA for the classes you entered.') > outfile = open('grades,txt'. 'r') > total = 0.0 > count = 0 > class_or_exit = outfile.readline() > grade = float(outfile:readline()) > > if grade >= 90. > grade = 4:0 > count = count + 1 > print(f"{class_or_exit:} class" + str + "{grade.:2f}") > elif grade >= 80. > grade = 3:0 > count = count + 1 > print(f'{class_or_exit:} class'+ str + "{grade.:2f}") > elif grade >= 80. > grade = 2:0 > count = count + 1 > print(f'{class_or_exit:} class'+ str + "{grade.:2f}") > elif grade >= 80. > grade = 1:0 > count = count + 1 > print(f'{class_or_exit:} class'+ str + "{grade..2f}") > > main()

This does what I think you meant to do.这就是我认为你想要做的。 Note carefully the differences between this and what you wrote.请仔细注意这与您所写的内容之间的差异。

def create():
    outfile = open('grades.txt', 'w')

    #Gather class name or press 'Enter' to quit
    class_or_exit = input('Enter course name or Enter to quit: ')

    #Create While loop if 'Enter' is pressed exit and close file
    while class_or_exit !="":
        grade =(input('Enter grade (interger) achieved: '))
        #Write info to the file
        outfile.write(class_or_exit + '\n')
        outfile.write(grade + '\n')
        class_or_exit = input('Enter course name or Enter to quit: ')

    #Close file
    outfile.close()
    print('File was created and closed')
    return grade, class_or_exit
    
def retrieve():
    print('Here is your GPA for the classes you entered:')
    outfile = open('grades.txt', 'r')
    total = 0.0
    count = 0
    while True:
        class_or_exit = outfile.readline()
        if not class_or_exit:
            break
        grade = float(outfile.readline())
        count += 1
    
        if grade >= 90:
            total += 4.0
        elif grade >= 80:
            total += 3.0
        elif grade >= 70:
            total += 2.0
        elif grade >= 60:
            total += 1.0

    print("Your GPA is", total/count)

def main ():
    create()
    retrieve()
    
main()

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

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