简体   繁体   中英

I don't know what is wrong with this python code

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

I'm currently learning 'Class' in python. And I can't find the wrong thing in this code. Is it wrong to use classes in other functions?

Appears to be a simple indentation error, this should be fine:

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
'''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM