Search code examples
pythonpython-3.xerror-handlingindentation

Static method is causing an unexpected indent error?


For some reason when I run this code I get an unexpected indent error on the @staticmethod line, but I have no idea why! Everything is properly indented, but this is causing all of my methods to raise errors in code post. Also, I removed a few other methods from the class to shorten the code I'm posting on here.

class Reservation:
    """A data type that represents a hotel room booking system."""
    booking_numbers = []

    @staticmethod
    def get_reservations_from_row(room_obj, list_tuples):
    
        reservation_dict = {}
        booking_num_list = []
        date_list = []
        for element in list_tuples:
            year, month, day, short_str = element
            list_short_str = short_str.split('--')
            booking_num = int(list_short_str[0])
            name = list_short_str[1]
        
            if booking_num not in booking_num_list:
                booking_num_list.append(booking_num)
      
            date = datetime.date(year, month, day)
        
            date_list.append(date)
         
        for element in booking_num_list:
            print(date_list)
            date_list.sort()
            value = Reservation(name, room_obj, date_list[0], date_list[len(date_list)-1], element)
    
            reservation_dict[element] = reservation_dict.get(element, []) + value
        return reservation_dict

Traceback (most recent call last): File "/Applications/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ast.py", line 35, in parse return compile(source, filename, mode, PyCF_ONLY_AST) File "/Users/myname/Downloads/reservation.py", line 74 @staticmethod ^ IndentationError: unexpected indent


Solution

  • It seems that you have used the tab for indentation. Remove the tab and insert 4 spaces it will work for you