Https://app.dataquest.io/m/351/cleaning-and-preparing-data-in-python/8/parsing-numbers-from-complex-strings-part-two,syntax error

Screen Link: https://app.dataquest.io/m/351/cleaning-and-preparing-data-in-python/8/parsing-numbers-from-complex-strings-part-two
Your Code: ‘def process_date(string):
if ‘-’ in string:
converted_string_list = string.split(’-’)
first_string = converted_string_list[0]
second_string = converted_string_list[1]
int_fir_string = int(first_string)
int_sec_string = int(second_string)
average = (int_fir_string + int_sec_string)/2
average = round(average)
return average
else :
int_string = int(string)
return int_string
processed_test_data =
for data in stripped_test_data:
data_processed = process_date(data)
processed_test_data.append(data_processed)
print(processed_test_data)
for row in moma:
Date = row[6]
Date =strip_characters(Date)
Date = process_date(Date)’
What I expected to happen:I expect that the code split the string if it contains the hyphen if any otherwise it just coverts it to integer. in the first case, after split it should convert it to integer and take the average. After that it is pass the testing phase using the stripped test data.

What actually happened: else : int_string = int(string) return int_string the trace back error says the syntax is not valid

Other details: when I try to remove this part in the debugging process to see what’s going on it was still abortive

Hi @adeyemiqudus361
Can you test this code and I see if you still getting the error message

    
def process_date(string):
    if '-' in string:
        converted_string_list = string.split('-')
        first_string = converted_string_list[0]
        second_string = converted_string_list[1]
        int_fir_string = int(first_string)
        int_sec_string = int(second_string)
        average = (int_fir_string + int_sec_string)/2
        average = round(average)
        return average
    else :
        int_string = int(string)
        return int_string
processed_test_data =[]
for data in stripped_test_data:
    data_processed = process_date(data)
    processed_test_data.append(data_processed)
print(processed_test_data)
for row in moma:
    Date = row[6]
    Date = strip_characters(Date)
    Date = process_date(Date)

I just solved the problem now. It was an indentation issue I will be glad if you can make some elaborations on that. on the ‘return average’ it was suppose to be properly indented to make the code work well. also the return statement immediately after else was not indented properly. can you please give some explanations on that?

Hi @adeyemiqudus361
Thank’s for the remark I had just corrected the indentation in return statement after else.

1 Like

I should thank you accordingly for your support, you have been of great help. Good to have you here

1 Like