Question

Simple Python code - no output

  • 12 June 2023
  • 5 replies
  • 128 views

i use this simple code (made by chatgpt):

def extract_data(body):
    lines = body.split('\n')  # Split the body text into lines
    first_name = last_name = None  # Initialize first_name and last_name as None

    for i in range(len(lines) - 1):  # Iterate over each line, leave out the last line
        if "שם פרטי" in lines[i]:  # If line contains "שם פרטי"
            first_name = lines[i + 1].strip()  # Extract the next line as first_name
        elif "שם משפחה" in lines[i]:  # If line contains "שם משפחה"
            last_name = lines[i + 1].strip()  # Extract the next line as last_name

    output = [{'first name': first_name, 'last name': last_name}]
    return output

 

i receive

output_missing

Please define output or return early.

id

oxoJXdKRLy3c5EwtrvzcLZ9A2ENs0kRl

runtime_meta

memory_used_mb

45

duration_ms

3

logs

 

 

why? 

what wrong?

 


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

5 replies

i also used this code:

def extract_data(input_data):
    body = input_data['body']
    lines = body.split('\n')  # Split the body text into lines
    first_name = None  # Initialize first_name as None
    last_name = None  # Initialize last_name as None

    for i in range(len(lines) - 1):  # Iterate over each line, leave out the last line
        if "שם פרטי" in lines[i]:  # If line contains "שם פרטי"
            first_name = lines[i + 1].strip()  # Extract the next line as first_name
        elif "שם משפחה" in lines[i]:  # If line contains "שם משפחה"
            last_name = lines[i + 1].strip()  # Extract the next line as last_name

    output = [{'first name': first_name, 'last name': last_name}]

    return output
 

 

i recieved

 

output_missing

Please define output or return early.

id

jeFhEYgByF3HQql8v4iyRQ2011AWwd5P

runtime_meta

memory_used_mb

45

duration_ms

1

logs

Userlevel 1
Badge +1

Hi,

 

Try changing the second last line

 

i.e 

 

output = [{'first name': first_name, 'last name': last_name}]

 

to 

 

output = {'first name': first_name, 'last name': last_name}

 

and see if the problem still persists?

 

i get the same result

 

i used this code

def extract_data(input_data):
    body = input_data['body']
    lines = body.split('\n')  # Split the body text into lines
    first_name = None  # Initialize first_name as None
    last_name = None  # Initialize last_name as None

    for i in range(len(lines) - 1):  # Iterate over each line, leave out the last line
        if "שם פרטי" in lines[i]:  # If line contains "שם פרטי"
            first_name = lines[i + 1].strip()  # Extract the next line as first_name
        elif "שם משפחה" in lines[i]:  # If line contains "שם משפחה"
            last_name = lines[i + 1].strip()  # Extract the next line as last_name

    output = {'first name': first_name, 'last name': last_name}

    return output

Userlevel 1
Badge +1

Oh I see now!
You have simply declared the function "extract_data" but in no way are you calling the function.

Try using the below code. 

 

 

 

 

 

    body = input_data['body']
    lines = body.split('\n')  # Split the body text into lines
    first_name = None  # Initialize first_name as None
    last_name = None  # Initialize last_name as None

    for i in range(len(lines) - 1):  # Iterate over each line, leave out the last line
        if "שם פרטי" in lines[i]:  # If line contains "שם פרטי"
            first_name = lines[i + 1].strip()  # Extract the next line as first_name
        elif "שם משפחה" in lines[i]:  # If line contains "שם משפחה"
            last_name = lines[i + 1].strip()  # Extract the next line as last_name

    output = {'first name': first_name, 'last name': last_name}

    return output
    
    

 

    
    
    

FYI It is your exact same code, but without the first line i.e "def extract_data(input_data):"
Hopefully this works for you.
 

work like charm. 

 

thanks!