Python Forum

Full Version: Indentationerror
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there
I have followed error:
Indentationerror: unindent does not match any outer indentation level


It is all about this line: if weekday != 'all': and this one: if month != 'all':

in this statement:
df = pd.read_csv(CITY_DATA[city])
def main():
    while True: city, month, weekday
        #For weekday
    df = load_data(city, month, weekday)
        # TO DO: display the most common month
     #convert start time into datetime
    df['Start Time'] = pd.to_datetime(df['Start Time'])
    #create a month column
    df['month'] = df['Start Time'].dt.month

    df['day_of_week'] = df['Start Time'].dt.weekday_name
    if weekday != 'all':
        # filter by day of week to create the new dataframe
        df = df[df['day_of_week'] == weekday.title()]
            
             # filter by month if applicable
     if month != 'all':
        # use the index of the months list to get the corresponding int
        months = ['January', 'February', 'March', 'April', 'May', 'June']
        month = months.index(month) + 1
if __name__ == "__main__":
    main()
How can I fix this?
The next if statement is not indented properly - if month != 'all':
Move it to the left one space.
I did:
df['month'] = df['Start Time'].dt.month

    df['day_of_week'] = df['Start Time'].dt.weekday_name
        if weekday !='all':
        # filter by day of week to create the new dataframe
            df = df[df['day_of_week'] == weekday.title()]
            
             # filter by month if applicable
        if month != 'all':
        # use the index of the months list to get the corresponding int
        months = ['January', 'February', 'March', 'April', 'May', 'June']
        month = months.index(month) + 1
now I have an error here: if weekday !='all':
Error Message: Unexpected indent

ok I got it. Thank you!
What was the issue?
moving the if statement to the left :)