2

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 :)

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Michelle McGuire
  • 77
  • 2
  • 3
  • 12

1 Answers1

5

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

Rahul
  • 10,830
  • 4
  • 53
  • 88
  • 2
  • 1
    Tuples are immutable, so they can **not** be changed, no exceptions. However, if a tuple contains mutable objects, you can mutate those objects (but you still can't assign to them). – PM 2Ring Jul 21 '17 at 07:02
  • @juanpa.arrivillaga : This exception. https://gist.github.com/pra007/fc0792519046780cebe23770633fc1b4#file-unexpected-tuple – Rahul Jul 21 '17 at 07:28
  • @Rahul that `tuple` has not been mutated. It contains exactly the same objects it always did. Indeed, the *reason* you get that error is because if the augmented assignment operator worked it would potentially mutate the `tuple`. See PM2Ring's comment – juanpa.arrivillaga Jul 21 '17 at 07:36
  • Since this answer solved the problem, you should [accept it](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – Jim K Jul 21 '17 at 07:45
  • @juanpa.arrivillaga: It's debatable. I don't give immutable mess, but Tuple has changed in that gist. That's it. – Rahul Jul 21 '17 at 08:04
  • @Rahul no, it *definitely hasn't*. The *list* contained in the `tuple` got mutated, not the `tuple`. – juanpa.arrivillaga Jul 21 '17 at 08:12
  • @juanpa.arrivillaga : `(1, 2, [30, 40]) != (1, 2, [30, 40, 50, 60])` – Rahul Jul 21 '17 at 08:14
  • That doesn't imply that the tuple was mutated. Please see [this post](https://stackoverflow.com/questions/9755990/why-can-tuples-contain-mutable-items). – juanpa.arrivillaga Jul 21 '17 at 08:25
  • Thanks. I am not saying it's mutated. It's changed. – Rahul Jul 21 '17 at 09:18