Python Forum
Class - Error Message (Indentation)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class - Error Message (Indentation)
#1
Hi,

I have a writing a Class definition. However, I got error msg: 

Error:
SyntaxError: inconsistent use of tabs and spaces in indentation
class Sunday:
                def __init__(self, dayname, start_date=datetime.today()):
                                self.dayname = dayname
                                self.date = start_date
                                self.day = start_date.day
                                self.month = start_date.month
                                self.year = start_date.year
                                self.weekday = start_date.weekday()
                                self.weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
                def get_Prev(self):
                                day_num = self.weekday
                                day_num_target = self.weekdays.index(self.dayname)
                                days_ago = (7 + day_num - day_num_target) % 7
                                if days_ago == 0:
                                                days_ago = 7
                                self.date = self.date - timedelta(days=days_ago)
                                self.day = self.date.day
                                self.month = self.date.month
                                self.year = self.date.year
                                self.weekday = self.date.weekday()
                def get_Arg(self):
                                self.date = self.get_Prev()
I'm not sure why I got the error message. The indentation seems consistent to me. The Class definition would work until I introducted the last 2 lines of code: 

def get_Arg(self):

                                self.date = self.get_Prev()
Wonder if someone could point out to me how I could resolve this.

Thank you!

After I retyped the whole thing, it works now.
Smile
Reply
#2
4 spaces shall indents be; no more; no less.

Thou shall not indent 3 spaces unless immediately followed by a fourth.
5 is right out.

class Sunday:
    def __init__(self, dayname, start_date=datetime.today()):
        self.dayname = dayname
        self.date = start_date
        self.day = start_date.day
        self.month = start_date.month
        self.year = start_date.year
        self.weekday = start_date.weekday()
        self.weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    def get_Prev(self):
        day_num = self.weekday
        day_num_target = self.weekdays.index(self.dayname)
        days_ago = (7 + day_num - day_num_target) % 7
        if days_ago == 0:
            days_ago = 7
        self.date = self.date - timedelta(days=days_ago)
        self.day = self.date.day
        self.month = self.date.month
        self.year = self.date.year
        self.weekday = self.date.weekday()
    def get_Arg(self):
        self.date = self.get_Prev()
Any editor worth using allows you to set the tab key to insert spaces rather than tabs.  If your editor doesn't allow this you need to use a different one.
Reply
#3
Hand Hand
Use 4 spaces per indentation level.
Reply
#4
Python will accept any indentation level even 32 spaces but 4 is a standart, the code is readable and doesn't waist the screen space
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error message about iid from RandomizedSearchCV Visiting 2 934 Aug-17-2023, 07:53 PM
Last Post: Visiting
  Another Error message. the_jl_zone 2 943 Mar-06-2023, 10:23 PM
Last Post: the_jl_zone
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,685 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  understanding error message krlosbatist 1 1,857 Oct-24-2021, 08:34 PM
Last Post: Gribouillis
  Error message pybits 1 36,077 May-29-2021, 10:26 AM
Last Post: snippsat
  f-string error message not understood Skaperen 4 3,268 Mar-16-2021, 07:59 PM
Last Post: Skaperen
  Overwhelmed with error message using pandas drop() EmmaRaponi 1 2,301 Feb-18-2021, 07:31 PM
Last Post: buran
  Winning/Losing Message Error in Text based Game kdr87 2 2,927 Dec-14-2020, 12:25 AM
Last Post: bowlofred
  error in class non_name092 1 1,860 Sep-02-2020, 05:42 PM
Last Post: bowlofred
  Don't understand error message Milfredo 2 1,995 Aug-24-2020, 05:00 PM
Last Post: Milfredo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020