Home python Strange Error in Python: NameError: Name Is Not Defined

Strange Error in Python: NameError: Name Is Not Defined

Author

Date

Category

Good day. Why Python says that the “Is Not Defined” function?

Main.py:

Class Parser:
  ACC = 0.0.
  REST = ''
  DEF PARSE (Self, S):
    Self.acc = 0.0.
    Self.Rest = S.
    Parse_Plusminus ()
    Return Self.acc.
  DEF PARSE_PLUSMINUS (SELF):
    CUR = PARSE_NUM ()
    While Len (Self.Rest) & gt; 0:
      IF NOT (Self.Rest [0] == '+' or self.rest [0] == '-'):
        Break
      SIGN = Self.Rest [0]
      Next_num = Parse_num (Self.Rest [1:])
      IF Sign == '+':
        Self.acc + = CUR + NEXT_NUM
      ELSE:
        Self.acc + = CUR - Next_Num
  DEF PARSE_NUM (SELF):
    Negative = False
    i = 0.
    IF Self.Rest [0] == '-':
      Negative = True.
      Self.Rest = Self.Rest [1:]
    While I & LT; Len (Self.Rest) and (Self.Rest [i] .isdigit () or self.rest [i] == '.'):
      I + = 1
    Num = -Float (Self.Rest [0: i]) If Negative ELSE Float (Self.Rest [0: I])
    self.rest = Self.Rest [i:]
    Return Num.
P = Parser ()
Print (P.Parse ("1 + 2"))


Answer 1, Authority 100%

Class Parser:
  ACC = 0.0.
  REST = ''
  DEF PARSE (Self, S):
    Self.acc = 0.0.
    Self.Rest = S.
    self.parse_plusminus () # Add Self
    Return Self.acc.
  DEF PARSE_PLUSMINUS (SELF):
    Cur = Self.parse_num () # Add Self
    While Len (Self.Rest) & gt; 0:
      IF NOT (Self.Rest [0] == '+' or self.rest [0] == '-'):
        Break
      SIGN = Self.Rest [0]
      self.rest = Self.Rest [1:] # Remove First Char? I Guess So.
      next_num = self.parse_num () # Add Self
      IF Sign == '+':
        Self.acc + = CUR + NEXT_NUM
      ELSE:
        Self.acc + = CUR - Next_Num
  DEF PARSE_NUM (SELF):
    Negative = False
    i = 0.
    IF Self.Rest [0] == '-':
      Negative = True.
      Self.Rest = Self.Rest [1:]
    While I & LT; Len (Self.Rest) and (Self.Rest [i] .isdigit () or self.rest [i] == '.'):
      I + = 1
    Num = -Float (Self.Rest [0: i]) If Negative ELSE Float (Self.Rest [0: I])
    self.rest = Self.Rest [i:]
    Return Num.
P = Parser ()
Print (P.Parse ("1 + 3")) # Output: 4.0

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions