Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单python类不工作_Python - Fatal编程技术网

简单python类不工作

简单python类不工作,python,Python,我在Python中遇到了一些问题,为什么它不工作 class Quiz: def __init__(self, answer, question): self.answer = answer self.question = question def yesno(self): if self.answer == self.question: return str("Correct!") else:

我在Python中遇到了一些问题,为什么它不工作

class Quiz:
    def __init__(self, answer, question):
        self.answer = answer
        self.question = question

    def yesno(self):
        if self.answer == self.question:
        return str("Correct!")
    else:
        return str("Wrong!")

print("Time for a quiz.")

print("What is ((((6^2 * 10) + sqrt((5000*3) - 600)) / 4! ) * 4 ) - log(1 * 10^11)?")
userAnswer = int(input())
question1 = Quiz(userAnswer, 69)
Quiz.yesno()

问题1.yesno()
可以


yesno()
是类的对象可以调用的方法。如果它是一个静态方法,
quick.yesno()
就可以工作。

是的,您需要实例化该类。因此,
question1.yesno()
是调用该方法的正确方法(您正在调用对象实例上的方法)

此外,您还有一个小的缩进错误,这可能会导致以后出现一些问题

def yesno(self):
        if self.answer == self.question:
        return str("Correct!")
    else:
        return str("Wrong!")
实际上应该是:

def yesno(self):
            if self.answer == self.question:
               return str("Correct!")
            else:
               return str("Wrong!")

问题1.yesno()如果self.answer==self.question:?@KalpeshDusane:它比较self.answer和self.question的值是否相同。当然,这些变量的命名是完全错误的。