LIMITED TIME OFFER: 50% OFF OF PREMIUM WITH OUR ANNUAL PLAN (THAT'S $294 IN SAVINGS).
GET OFFER

Stuck on page 8/10 Data Cleaning and Preparing in Python - "Value of moma is not what we expected"

Screen Link:
https://app.dataquest.io/m/351/cleaning-and-preparing-data-in-python/8/parsing-numbers-from-complex-strings-part-two

My Code:

test_data = ["1912", "1929", "1913-1923",
             "(1951)", "1994", "1934",
             "c. 1915", "1995", "c. 1912",
             "(1988)", "2002", "1957-1959",
             "c. 1955.", "c. 1970's", 
             "C. 1990-1999"]

bad_chars = ["(",")","c","C",".","s","'", " "]

def strip_characters(string):
    for char in bad_chars:
        string = string.replace(char,"")
    return string

stripped_test_data = ['1912', '1929', '1913-1923',
                      '1951', '1994', '1934',
                      '1915', '1995', '1912',
                      '1988', '2002', '1957-1959',
                      '1955', '1970', '1990-1999']

def process_date(date):
    if "-" in date:
        split_date = date.split("-")
        date_one = split_date[0]
        date_two = split_date[1]
        date = (int(date_one) + int(date_two))/2
        date = round(date)
    
    else:
        date = int(date)
        
        return date
    
processed_test_data = []

for d in stripped_test_data:
    date = process_date(d)
    processed_test_data.append(date)
    
for row in moma:
    date = row[6]
    date = strip_characters(date)
    date = process_date(date)
    row[6] = date

What I expected to happen:
To move onto the next page.

What actually happened:

Value of moma is not what we expected.

@gilleanf

Hi

I found a ‘if’ sentence without return, perhaps…

def process_date(date):
    if "-" in date:
        split_date = date.split("-")
        date_one = split_date[0]
        date_two = split_date[1]
        date = (int(date_one) + int(date_two))/2
        date = round(date)
        return date   # <- this is what I found. 

    else:
        date = int(date)
        
        return date

Let me say if works.

A&E

Thank you so much! That worked.