MyProLab wont accept my code. Help please!

I am working on homework and have tried many variations of this code but MyProgrammingLab
doesn’t seem to accept any of them. I am told it needs an “if statement” but I am so burnt out and not really sure where it should go at this point.

The question is this:

Blockquote
Write a program that reads the records from the golf.txt file written in Exercise
10a and prints them in the following format:

Name: Emily
Score: 30

Name: Mike
Score: 20

Name: Jonathan
Score: 23

This is what I have

def main():
    data = open('golf.txt', 'r')

    name = data.readline()

    name = name.rstrip('\n')
    score = ''
    while name !='':

        score = data.readline()

        print('Name:', name)
        print('Score:', score)

        name = data.readline()

        name = name.rstrip('\n')
    data.close()
main()
1 Like

The requirements don’t mention any if statements, they could be hinting at something you learned in class. Perhaps they want you to check IF the file was opened and handle it otherwise?
Impossible for me to say for sure though

Yeah, I was very confused because I asked my professor why none of my codes would work,
and she said it was looking for an if statement. But I wasn’t sure how that would tie in at all in this case.

There is another part to the question though in the book that wasn’t given in myprolab that I found in my book. If this helps:

Blockquote
you are asked to write two programs.
1)a program that will read each players name’s name and golf score as keyboard input, then save these as records in a file named golf.txt (Each record will have a field for the player’s name and a field for the player’s score).

2: a program that reads the records form the golf.txt file and displays them.
(this is what my prolab is asking for)

Should I write a program that includes both for this assignment? Even though on myprolab it only asks for #2??