将文本和整数写入文件,然后从文件中读取以计算平均成绩
Writing text and intergers to a file, then reading from the file to calculate grade averages
这就是我要显示的内容。这些是 2 个函数的副产品。
第一段是创建“grades.txt”文件的 create() 函数。我想我很容易就倒下了。
其次,下一段来自“Retrieve()”函数,从“grades.txt”文件中读取并显示 class 姓名和 GPA,然后计算那些 classes 并显示它们。
我觉得我正处于风口浪尖,但我不知道从哪里开始,因为我被“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()
这就是我认为您想要做的。请仔细注意这与您所写内容之间的差异。
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()
这就是我要显示的内容。这些是 2 个函数的副产品。 第一段是创建“grades.txt”文件的 create() 函数。我想我很容易就倒下了。
其次,下一段来自“Retrieve()”函数,从“grades.txt”文件中读取并显示 class 姓名和 GPA,然后计算那些 classes 并显示它们。 我觉得我正处于风口浪尖,但我不知道从哪里开始,因为我被“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()
这就是我认为您想要做的。请仔细注意这与您所写内容之间的差异。
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()