I copied the example from the Internet, Pycharm gives the error: "Statement expected, found BAD_CHARACTER". What could be the problem?

class Person:     def __init__(self, name, age):         self.__name = name   # устанавливаем имя         self.__age = age     # устанавливаем возраст      def set_age(self, age):         if age in range(1, 100):             self.__age = age         else:             print("Недопустимый возраст")      def get_age(self):         return self.__age              def get_name(self):         return self.__name      def display_info(self):         print("Имя:", self.__name, "\tВозраст:", self.__age)          tom = Person("Tom", 23)  tom.__age = 43             # Атрибут age не изменится tom.display_info()         # Имя: Tom Возраст: 23 tom.set_age(-3486)         # Недопустимый возраст tom.set_age(25) tom.display_info()         # Имя: Tom Возраст: 25 
  • In what place swears? I copied your example and pycharm did not complain (although he did the correct indents). Make sure that you have 4 spaces indents - gil9red
  • There were problems with spaces, I already solved the problem - The Elusive Joe
  • one
    And at the beginning of the file you have the line # -*- coding: utf-8 -*- ? - gil9red
  • @ gil9red, there is no such line - The Elusive Joe
  • Add and try your code from the question. Perhaps this was the problem - gil9red

2 answers 2

Recommendations:

  • Make a single indent (4 spaces)
  • Add a line to the beginning of the file: # -*- coding: utf-8 -*-

Code:

 # -*- coding: utf-8 -*- class Person: def __init__(self, name, age): self.__name = name # устанавливаем имя self.__age = age # устанавливаем возраст def set_age(self, age): if age in range(1, 100): self.__age = age else: print("Недопустимый возраст") def get_age(self): return self.__age def get_name(self): return self.__name def display_info(self): print("Имя:", self.__name, "\tВозраст:", self.__age) tom = Person("Tom", 23) tom.__age = 43 # Атрибут age не изменится tom.display_info() # Имя: Tom Возраст: 23 tom.set_age(-3486) # Недопустимый возраст tom.set_age(25) tom.display_info() # Имя: Tom Возраст: 25 

    The indents were not for 4 spaces, and the comments # устанавливаем имя and # устанавливаем возраст had to be removed after everything worked.