我不知道这个python代码有什么问题

时间:2019-02-16 10:08:09

标签: python function

class FC():
    def data(self, a, b):
        self.n1 = a
        self.n2 = b

    def add(self):
        return (self.n1 + self.n2)
    >>> def pbnc(start, num):
        pb = FC()
        pb.data(start, start)
        while (num > 0):
            print(pb.add())
            pb.data(pb.n2, pb.add())
            num -= 1


>>> def pbnc(1, 10)
SyntaxError: invalid syntax

我目前正在使用python学习“课程”。而且我在这段代码中找不到错误的内容。在其他函数中使用类是否错误?

1 个答案:

答案 0 :(得分:0)

似乎是一个简单的缩进错误,应该没问题:

class FC():
    def data(self, a, b):
        self.n1 = a
        self.n2 = b

    def add(self):
        return (self.n1 + self.n2)

    def pbnc(self, start, num):
        pb = FC()
        pb.data(start, start)
        while (num > 0):
            print(pb.add())
            pb.data(pb.n2, pb.add())
            num -= 1
'''
# Uncomment this part if you want this method outside of the class
def pbnc(start, num):
    pb = FC()
    pb.data(start, start)
    while (num > 0):
        print(pb.add())
        pb.data(pb.n2, pb.add())
        num -= 1
'''