Home>

Resolve indentation problems and try class class methods and instance methods
Please write the details of the question here.
I started python today but I'm not sure about the indentation problem
The following error message occurred while implementing the ■■ function.

Error message

File"try.py" ;, line 25
print ("{0} instances".format (cls.count))
^
IndentationError: expected an indented block

Applicable source code

python Enter the language name here
Source code

class User:
count = 0

definit(self, name):
variable
User.count + = 1
self.name = name

def say_hi (self):
print ("hi {0}".format (self.name))

@classmethod
def show_info (cls):
print ("{0} instances".format (cls.count))

tom = User ("tom")

bob = User ("bob")

print (tom.count)

bob.say_hi ()
User.show_info ()

Tried

I want the number of instances and hi bob in the Python class
How does it work?
Professor

Supplemental information (FW/tool version etc.)

python3

Please provide more detailed information here.

  • Answer # 1

    Do you indent the processing in def? If you haven't done so, you may want to fix it. I think that the place where you call the function should be the current indentation.

  • Answer # 2

    Is the indentation the following?

    class User:
        count = 0
        def __init __ (self, name):
            # variable
            User.count + = 1
            self.name = name
        def say_hi (self):
            print ("hi {0}". format (self.name))
        @classmethod
        def show_info (cls):
            print ("{0} instances" .format (cls.count))
    tom = User ("tom")
    bob = User ("bob")
    print (tom.count)
    bob.say_hi ()
    User.show_info ()
    How to write code

    StackOverflow has a function that makes it easy to see the code as shown above.
    Open the question edit screen and press the<code>button with the code selected.