Michelle McGuire
Michelle McGuire

Reputation: 77

What does AttributeError: 'tuple' object has no attribute 'append' and how do I fix my code?

I'm stuck on an assignment from my professor. It's asking me to do the following:

Write a program in Python that will grade user's answers to a driver's license test exam that is consist of ten multiple choice questions.

The correct answers for question 1 to question can be stored in a list called correct_answers with these initial values:

correct_answers=['B','D','C','B','C','D','A','B','D','A']

Your program should prompt user to enter his/her answers for the 10 questions in a single line separated by a blank. Once the user press the Enter key, build a list of answers and Lab #5 explains how to do this.

You can, if you want to, store your answers from a list instead of reading them from the keyboard. This will save a lot of time as you don't need to enter the answers when you run your program. You should change your answers though just for testing purposes.

Once you have your list of answers, compare each value to the list correct_answers and keep a count of how many were correct.

Lastly, display the number of correct answers out of 10 and also display the %. So if 5 answers were correct, you should display 5 correct answers and that is 50%

Also note that you must use functions() to solve this program.

Here's my code:

def read_student():
    contents = ()
    for x in range (0,10):
        data = input('Enter your answers for the 10 questions in a 
single line separated by a blank')
        contents.append(data)
    return contents 

def pass_fail(correct_answers, student_answers):
    num_correct = 0
    for i in range(0, len(correct_answers)):
        if correct_answers[i] == student_answers[i]:
            num_correct = num_correct + 1

    print("You got %d answers correct" % num_correct)
    percent_correct = (num_correct / 10 ) * 100
    print("The percentage of correct answers is %d" % 
percent_correct)


correct_answers = ['B', 'D', 'C', 'B', 'C', 'D', 'A', 'B', 'D', 'A']
student_answers = read_student()
pass_fail(correct_answers, student_answers)

It keeps saying that line 5 (contents.append(data)) has a AttributeError: 'tuple' object has no attribute 'append'...if just not sure what it means or how to fix it. Any help/resources would be greatly appreciated. Thanks :)

Upvotes: 2

Views: 12505

Answers (1)

Rahul
Rahul

Reputation: 11550

Tuple is imutable data type means you can not change it. (with some exception) One thing you can do is change contents = () to contents = []

Upvotes: 5

Related Questions