“type”对象不是subscriptable python

2024-10-03 21:28:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道这个错误告诉我什么,它说这个错误是在64行的这段代码如果可能的话,请你也检查一下,看看我的代码是否有额外的错误thnx这么多的人,我尝试了很多事情,但无法让它工作。你知道吗

Traceback (most recent call last):
  File "C:\Users\AdamFeffer\triviacrack.py", line 93, in <module>
    QnA2()
  File "C:\Users\AdamFeffer\triviacrack.py", line 64, in QnA2
    if uanswer == quest[whichq].rA:
TypeError: 'type' object is not subscriptable

import random
import time
# Flase = playerone true player 2
wt = False
score = 0
kg = True
kgt = True
playerov = False
class playerone(object):
        def __init__(self, name, score, victory):
                self.name = name
                self.score = score
                self.victory = victory
class playertwo(object):
        def __init__(self, name, score, victory):
                self.name = name
                self.score = score
                self.victory = victory
class quest(object):
    def __init__(self, q, a1, a2, a3, a4, rA):
        self.question = q
        self.answer1 = a1
        self.answer2 = a2
        self.answer3 = a3
        self.answer4 = a4
        self.realanswer = rA
### Questions ###
q1 = quest("The largest artery in the human body helps carry blood from the heart throughout the body. What is it?","Aorta", "your mom haha lol", "Jugular","Borta", 1)
q2 = quest("In the year 1900 in the U.S. what were the most popular first names given to boy and girl babies?", "William and Elizabeth", "Joseph and Catherine", "John and Mary","George and Anne", 3)
q3 = quest("When did the Liberty Bell get its name?", "when it was made, in 1701", "when it rang on July 4, 1776", "in the 19th century, when it became a symbol of the abolition of slavery", " none of the above", 3)
#################
#questoin arrays
questions = [q1, q2, q3]
qt = ["science", "history", "sports", "entertainment", "art"]
def QnA():
        global kg
        global oneplayer
        global score
        global player
        p = player
        whichq = random.randint(0, 4)
        print(questions[whichq].question)
        print(questions[whichq].answer1 + " (1)")
        print(questions[whichq].answer2 + " (2)")
        print(questions[whichq].answer3 + " (3)")
        print(questions[whichq].answer4 + " (4)") 
        uanswer = int(input("answer "))
        if uanswer == quest[whichq].realanswer:
                score = score + 1
                print ("YA NEXT QUESTION")
        else:
                print(p + "you didn't get it right")
                kg = False
def QnA2():
        global kgt
        global wt
        whichq = random.randint(0, 4)
        print(questions[whichq].question)
        print(questions[whichq].answer1 + " (1)")
        print(questions[whichq].answer2 + " (2)")
        print(questions[whichq].answer3 + " (3)")
        print(questions[whichq].answer4 + " (4)") 
        uanswer = int(input("answer "))
        if uanswer == quest[whichq].rA:
                if wt == false:
                        playerone.score = playerone.score + 1
                else:
                        playertwo.score = playertwo.score + 1
        if wt == True:
                wt = False
        else:
                wt = True
def timer():
        timer = 60
        while timer > 0:
                print (timer)
                timer = timer - 1
                time.sleep(1)
        print ("times up!!")
oot = int(input("(1) one player sudden death or (2) 2 player: "))
if oot == 1:
        oneplayer = True
        player = input("player name: ")
        while kg == True:
                QnA()
        print("sorry your score was " + (score) + "gg")
else:
        playero = input("player 1 name: ")
        playert = input("player 2 name: ")
        playerone = playerone(playero, 0, False)
        playertwo = playertwo(playert, 0, False)
        while kgt == True:
                QnA2()

Tags: thenameinselffalsetrueifquestions
1条回答
网友
1楼 · 发布于 2024-10-03 21:28:44

解决方案

第64行:if uanswer == questions[whichq].realanswer:

有什么问题吗

您正在尝试使用quest,它是一个类,而不是questions列表。quest不能用[]下标,但questions可以下标,因为它是一个列表。你知道吗

此外,您正在尝试使用rA,它是__init__函数的一个参数。不能在__init__之外使用此参数。相反,您需要使用成员变量realanswer:)

相关问题 更多 >