Python Forum
Indentationerror - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Indentationerror (/thread-10159.html)



Indentationerror - Jack_Sparrow - May-15-2018

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?


RE: Indentationerror - wavic - May-15-2018

The next if statement is not indented properly - if month != 'all':
Move it to the left one space.


RE: Indentationerror - Jack_Sparrow - May-15-2018

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!


RE: Indentationerror - wavic - May-15-2018

What was the issue?


RE: Indentationerror - Jack_Sparrow - May-16-2018

moving the if statement to the left :)