#๐Ÿ”’ "return" can be used only within a function

14 messages ยท Page 1 of 1 (latest)

umbral frigate
#

my code if you see the error let me know :#cont :()
DI ='0123456789'
#:) tokens
TT_INT = 'TT_INT'
TT_FLOAT = 'FLOAT'
TT_PLUS = 'PLUS'
TT_MINUS = 'MINUS'
TT_MUL = 'MUL'
TT_DIV = 'DIV'
TT_LPAREN = 'LPAREN'
TT_RPAREN = 'RPAREN'
class Token:
def init(self,type_, value):
self.type = type_
self.value = value
def repr(self):
if self.value: return f'{self.type}'
#lexer :3
class Lexer:
def init(self,text):
self.text = text
self.pos = -1
self.current_char = None
def advance(self,pos):
self.pos +=1
self.current_char = self.text[pos] if self.pos < len(self.text)else None
def make_tokens(self):
tokens = []
while self.current_char != None:
if self.current_char in ' \t':
self.advance()
elif self.current_char in DI:
tokens.append(self.make_number())
elif self.current_char == '+':
tokens.append(Token(TT_PLUS))
self.advance()
elif self.current_char == '-':
tokens.append(Token(TT_MINUS))
self.advance()
elif self.current_char == '*':
tokens.append(Token(TT_MUL))
self.advance()
elif self.current_char == '/':
tokens.append(Token(TT_DIV))
self.advance()
elif self.current_char == '(':
tokens.append(Token(TT_LPAREN))
self.advance()
elif self.current_char == ')':
tokens.append(Token(TT_RPAREN))
return tokens
def make_num(self):
num_str = ''
dot_count = 0
while self.current_char != None and self.current_char in DI + '.':
if self.current_char == '.':
if dot_count == 1: break
dot_count += 1
num_str += '.'
else:
num_str += self.current_char

exotic remnantBOT
#

@umbral frigate

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

umbral frigate
#

if dot_count == 0:

   return Token(TT_INT, int(num_str))
else:
   return  Token(TT_FLOAT, float(u))
#

#lexer :3
class Lexer:
def init(self,text):
self.text = text
self.pos = -1
self.current_char = None

def advance(self,pos):
    self.pos +=1
    self.current_char = self.text[pos] if self.pos < len(self.text)else None
def make_tokens(self):
    tokens = []
    while  self.current_char != None:
     if self.current_char in ' \t':
        self.advance()
     elif self.current_char in DI:
        tokens.append(self.make_number())
     elif self.current_char == '+':
        tokens.append(Token(TT_PLUS))
        self.advance()
     elif self.current_char == '-':
        tokens.append(Token(TT_MINUS))
        self.advance()
     elif self.current_char == '*':
        tokens.append(Token(TT_MUL))
        self.advance()
     elif self.current_char == '/':
        tokens.append(Token(TT_DIV))
        self.advance()
     elif self.current_char == '(':
        tokens.append(Token(TT_LPAREN))
        self.advance()
     elif self.current_char == ')':
        tokens.append(Token(TT_RPAREN))







    return tokens

def make_num(self):
   num_str = ''
   dot_count = 0

   while self.current_char != None and self.current_char in DI + '.':
      if self.current_char == '.':
         if dot_count == 1: break
         dot_count += 1
         num_str += '.'
      else:
         num_str += self.current_char

  
if dot_count == 0:

   return Token(TT_INT, int(num_str))
else:
   return  Token(TT_FLOAT, float(u))
pliant veldt
#

Hello
you can use

print("hello world") 

for better code readnes here, like in screenshot

lunar marlin
#

Or you can paste your code here

#

!paste

exotic remnantBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

lunar marlin
#

To help you we will need you to do one of the things above so that your code is readable

lone birch
#
DI ='0123456789'
#:) tokens
TTINT      = 'TT_INT'
TT_FLOAT   = 'FLOAT'
TT_PLUS    = 'PLUS'
TT_MINUS   = 'MINUS'
TT_MUL     = 'MUL'
TT_DIV     = 'DIV'
TT_LPAREN = 'LPAREN'
TT_RPAREN = 'RPAREN'

class Token:
    def init(self,type, value):
        self.type = type_
        self.value = value
    def repr(self):
       if self.value: return f'{self.type}'

#lexer :3
class Lexer:
    def init(self,text):
        self.text = text
        self.pos = -1
        self.current_char = None

    def advance(self,pos):
        self.pos +=1
        self.current_char = self.text[pos] if self.pos < len(self.text)else None

    def make_tokens(self):
        tokens = []

        while  self.current_char != None:
         if self.current_char in ' \t':
            self.advance()
         elif self.current_char in DI:
            tokens.append(self.make_number())
         elif self.current_char == '+':
            tokens.append(Token(TT_PLUS))
            self.advance()
         elif self.current_char == '-':
            tokens.append(Token(TT_MINUS))
            self.advance()
         elif self.current_char == '*':
            tokens.append(Token(TT_MUL))
            self.advance()
         elif self.current_char == '/':
            tokens.append(Token(TT_DIV))
            self.advance()
         elif self.current_char == '(':
            tokens.append(Token(TT_LPAREN))
            self.advance()
         elif self.current_char == ')':
            tokens.append(Token(TT_RPAREN))

        return tokens

    def make_num(self):
       num_str = ''
       dot_count = 0

       while self.current_char != None and self.current_char in DI + '.':
          if self.current_char == '.':
             if dot_count == 1: break
             dot_count += 1
             num_str += '.'
          else:
             num_str += self.current_char


       if dot_count == 0:
           return Token(TT_INT, int(num_str))
       else:
           return  Token(TT_FLOAT, float(u))

#

tried formatting it for u

worldly scarab
exotic remnantBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.