简体   繁体   English

AttributeError:“ tuple”对象没有属性“ append”是什么,如何修复我的代码?

[英]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. 用Python编写一个程序,该程序将为用户对驾驶执照考试的答案评分,该考试由十个多项选择题组成。

The correct answers for question 1 to question can be stored in a list called correct_answers with these initial values: 问题1到问题的正确答案可以使用以下初始值存储在名为correct_answers的列表中:

correct_answers=['B','D','C','B','C','D','A','B','D','A'] 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. 您的程序应提示用户在一行中用空格分隔的10个问题输入答案。 Once the user press the Enter key, build a list of answers and Lab #5 explains how to do this. 用户按下Enter键后,构建答案列表,然后练习5解释如何执行此操作。

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. 获得答案列表后,将每个值与列表correct_answers进行比较,并计算出多少个正确答案。

Lastly, display the number of correct answers out of 10 and also display the %. 最后,显示正确答案的数量(共10个),并显示%。 So if 5 answers were correct, you should display 5 correct answers and that is 50% 因此,如果5个答案正确,则应显示5个正确答案,即50%

Also note that you must use functions() to solve this program. 另请注意,必须使用functions()来解决此程序。

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. 它一直说第5行(contents.append(data))具有AttributeError:'tuple'对象没有属性'append'...如果只是不确定它的含义或解决方法。 Any help/resources would be greatly appreciated. 任何帮助/资源将不胜感激。 Thanks :) 谢谢 :)

Tuple is imutable data type means you can not change it. 元组是不可更改的数据类型,意味着您无法更改它。 (with some exception) One thing you can do is change contents = () to contents = [] (有一些例外),您可以做的一件事是将contents = ()更改为contents = []

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError: 'tuple' 对象没有属性 'append' - AttributeError: 'tuple' object has no attribute 'append' 如何修复 AttributeError: 'bytes' object has no attribute 'encode'? - How do I fix AttributeError: 'bytes' object has no attribute 'encode'? 如何修复 AttributeError: 'NoneType' object 没有属性 'lower'? - How do i fix AttributeError: 'NoneType' object has no attribute 'lower'? 如何修复此 AttributeError: 'SubRequest' object 没有属性 'getfuncargvalue'? - How do I fix this AttributeError: 'SubRequest' object has no attribute 'getfuncargvalue'? 我该如何解决这个 AttributeError: 'NoneType' object has no attribute 'text'? - How do i fix this AttributeError: 'NoneType' object has no attribute 'text'? 'tuple' 对象在我的 Python 代码中没有属性 'position'。 我该如何解决? - 'tuple' object has no attribute 'position' in my Python code. How can I fix it? 当 str 对象属性为“只读”时,这意味着什么? 如何修复我的代码? - What does it mean when a str object attribute is "read-only?" How do I fix my code? Python列表追加到列表“ AttributeError:'tuple'对象没有属性'append'” - Python list append to list “AttributeError: 'tuple' object has no attribute 'append'” AttributeError: 'tuple' object 没有以下代码的属性 - AttributeError: 'tuple' object has no attribute for the code below 如何将值附加到字典键? (AttributeError:“ str”对象没有属性“ append”) - How do I append a value to dict key? (AttributeError: 'str' object has no attribute 'append')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM