Ayman Afeef
Ayman Afeef

Reputation: 15

My code won't append student grades into a text file

I'm trying to write a program that allows a teacher to input the name of a student and the grade they recieved and then output that to a text file. After doing that 12 times, the code is supposed to ask you for the name of the file that has the grades, known as "grades.txt," and after you type in grades.txt it's supposed to read out the grades and the names of the students you entered.

count = 0
with open('grades.txt', 'w') as grades:
        grades.write('Student Names and Grades:')
        grades.write('\n')


while count <12:
    count+=1
    name=input(' Enter student name: ')
    try:
        average=int(input('Enter grade average: '))
        if 0<= average <=100:
            continue
        else:
            print('Average must be between 0 and 100')
    except ValueError:
        print ('Input grade must be a number')


     
    with open('grades.txt', 'a') as grades:
        grades.write('Student name: ')
        grades.write(name)
        grades.write(' -- Student average grade: ')
        grades.write(str(average))
        grades.write('\n')
        grades.close()


try:
    question=input('Enter file name for grades: ')
    if question == 'grades.txt':
        with open('grades.txt', 'r') as grades:
            grades_read = grades.read()
            print(grades_read)
            grades.close()
except:
   print('File not found')
  

However, when the file is read out, it only displays "Student Names and Grades:" and none of the names and grades that were appended to it. Does anyone know why?

Upvotes: 1

Views: 475

Answers (2)

Apo
Apo

Reputation: 338

This is because of the line

if 0<= average <=100:
        continue

continue will make you skip the rest of the code and go to the next iteration of the loop (so no printing in file here). The correct command here for your application is "pass" not "continue" just replace and try it out.

BTW : even if you put count < 12 its will only count 11 (maybe try <=)

Upvotes: 3

Sven Eberth
Sven Eberth

Reputation: 3116

Your continue is misplaces, you skip when you have a correct grade.

Furthermore you don't need to close a file, when you use the context manager (the with statement). And better use except FileNotFoundError than a bare except. And probably you want to increase count only after you saved a valid record, so move it to the end of the loop.

count = 0
with open('grades.txt', 'w') as grades:
    grades.write('Student Names and Grades:')
    grades.write('\n')

while count < 12:
    name = input(' Enter student name: ')
    try:
        average = int(input('Enter grade average: '))
        if not 0 <= average <= 100:
            print('Average must be between 0 and 100')
            continue
    except ValueError:
        print('Input grade must be a number')
        continue

    with open('grades.txt', 'a') as grades:
        grades.write('Student name: ')
        grades.write(name)
        grades.write(' -- Student average grade: ')
        grades.write(str(average))
        grades.write('\n')

    count += 1

try:
    question = input('Enter file name for grades: ')
    if question == 'grades.txt':
        with open('grades.txt', 'r') as grades:
            grades_read = grades.read()
            print(grades_read)
except FileNotFoundError:
    print('File not found')

Upvotes: 0

Related Questions