Python Forum
Get syntax error. Dont understand why
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get syntax error. Dont understand why
#11
The initializer for the clock class is given on line 6. It has eight parameters defined: self (which is given automatically), and seven others specifying a point in time (year, month, and so on).

On line 221, you create a clock instance, but provide no parameters. The self parameter is provided automatically, as I said. But the rest you need to provide on initialization. Either that, or you need to define default values for all of those parameters.

In the future, please give the full text of any error.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#12
Does anyone have any docs on the first line?
specifically the
init python:
# and 


label start:
Reply
#13
I have a idea for the code in the original post.
You repeat yourself. With some tricks,
you write code once and then you enter the data.

DAYTIMES = {
    'midnight': (23, 1),
    'day': (6, 20),
}


def overlapping_in_range(value, min_val, max_val): 
    if min_val < max_val:
        return min_val <= value < max_val
    elif min_val > max_val:
        return value >= min_val or value < max_val
        
def get_daytime(hour):
    for daytime, (mmin, mmax) in DAYTIMES.items():
        if overlapping_in_range(hour, mmin, mmax):
            return daytime
    return ''
[(x, get_daytime(x)) for x in range(25)]
Output:
[(0, 'midnight'), (1, ''), (2, ''), (3, ''), (4, ''), (5, ''), (6, 'day'), (7, 'day'), (8, 'day'), (9, 'day'), (10, 'day'), (11, 'day'), (12, 'day'), (13, 'day'), (14, 'day'), (15, 'day'), (16, 'day'), (17, 'day'), (18, 'day'), (19, 'day'), (20, ''), (21, ''), (22, ''), (23, 'midnight'), (24, 'midnight')]
Have to sleep now.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#14
(May-13-2019, 12:22 PM)ichabod801 Wrote: On line 221, you create a clock instance, but provide no parameters. The self parameter is provided automatically, as I said. But the rest you need to provide on initialization. Either that, or you need to define default values for all of those parameters.

Thank for ur advice
Can u show me some simple example
Not sure what is initialization guess is after __init__ but i tried
class Clock(self,2019,12,7etc.

tried
class Clock (self,year etc
self.year =2019

Reading about classes giv me no progres still no understand
how "define default values for all of those parameters" how to do a default values?
Yes i am not programmer

(May-13-2019, 10:51 PM)DeaD_EyE Wrote: You repeat yourself. With some tricks,
you write code once and then you enter the data.
I wish to use your code it is what i need but how to implement seasons?
Did u see that in winter the sun get up later and ealier come night
i mean that i am not soo advance to implement my ideas so i do the shovel work
can u consider this in ur code and after show me how to use it ?
i have tons of code which i am not able to operate because i have almost no programming knowledge
Sorry for this.
I v been tried many times
Every single error is much dissapointing and make morale low.
But i still try even if i am leech


I can imagine how often you, great people, meet such a not smart people like me.
Sry for I(Me)
Reply
#15
This is a clock instance for right now:

now = clock(2019, 5, 14, 1, 7, 0, 38)
Note that in your code you have clock = clock(). But clock is the name of the class. That line reassigns to clock name to an instance of the class. That means you no longer have access to the class itself. You need to be careful with your names.

To define defaults, you assign a value to the parameter in the method declaration:

def __init__(self , year = 1997, month = 8, day = 29, weekday = 4, hour = 2, minute = 14, second = 0)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#16
Ok Wink
So i make what u said
Make default
ang sligtly changed naming
to have this style
init python:
    class clock(object):
        def __init__(self, day = 1, hour = 8, minute = 0):
            self._day = day
            self._hour = hour
            self._minute = minute

        def addtime(self, minutes, hours = 0, days = 0): # use this to advance time
            self._minute += minutes # add minutes
            self._hour += hours # add hours
            self._day += days

            # add hours by floor division minute over 60
            self._hour += self._minute // 60

            # add days by floor division hour over 24 - make sure to do this after hour's been added
            self._day += self._hour // 24

            # now we can clean up minutes and hours by using mod %
            self._minute = self._minute % 60
            self._hour = self._hour % 24

        # use property to return formatted hour and minutes
        @property
        def day(self): # day as int
            return self._day

        @property
        def hour(self): # hour as int
            return self._hour

        @property
        def hh(self): # hours as HH. I'm not familiar enough with python to know if there's a shorter way to do this
            hour = "0" + str(self._hour)
            return hour[-2:]

        @property
        def minute(self): # minute as int
            return self._minute

        @property
        def mm(self): # minutes as mm.
            minute = "0" + str(self._minute)
            return minute[-2:]

default clock = clock()

label start:
    while True:
        "Day: [clock.day] Time: [clock.hh]:[clock.mm]"
        $ clock.addtime(15) # add 15 minutes
    return
But i have little problem with seasons here
Code says it is not defined Confused

NameError: global name '_season' is not defined

init python:
    weekdays = ("mon","tue","wed","thu","fri","sat","sun")
    daytimes = ("midnight","night","dawn","morning","noon","afternoon","dusk","night")
    seasons = ("winter", "spring", "summer", "autumn")
    class Clock(object):
        def __init__(self , year, month, day, weekday, hour, minute, second, season):
            self._year = year
            self._month = month
            self._day = day
            self._weekday = weekday
            self._hour = hour
            self._minute = minute
            self._second = second
            self._season = season
            
        def addtime(self , seconds , minutes , hours):
            self._second += seconds 
            self._minute += minutes # add minutes
            self._hour += hours

            self._minute += self._second // 60
            self._hour += self._minute // 60
            self._day += self._hour // 24
            
            self._second = self._second % 60
            self._minute = self._minute % 60
            self._hour = self._hour % 24
        
            self._weekday = weekdays[weekdays.index(self._weekday)-6]
            
            if self._month in (1,3,5,7,8,10,12):
                if self._day > 31:
                    self._day = 1
                    self._month += 1
            elif self._month in (4,6,9,11):
                if self._day > 30:
                    self._day = 1
                    self._month += 1
           
        #february in leap year
            else :
                # devide by 4
                if (self._year // 4) == 0 :
                    # devide by 100
                    if (self._year // 100) == 0 :
                        # devide by 400
                        if (self._year // 400) == 0 :
                            #leap year
                            __d = 29
                        else:
                            #normal year
                            __d = 28
                    else:
                        #devide by 4 - leap year
                        __d = 29
                else:
                    #normal year
                    __d = 28

                if self._day > __d:
                    self._day = 1
                    self._month += 1
            if self._month > 12:
                self._month = 1
                self._year += 1

        def daytime(self, daytime, season):
#("midnight","night","dawn","morning","noon","afternoon","dusk","night")
            if self._month in (12,1,2):
                self._season = seasons[0]
                if self._hour == 0:
                    return daytime[0]
                if self._hour > 0 and self._hour < 7:
                    return daytime[1]
                if self._hour > 7 and self._hour < 8:
                    return daytime[2]
                if self._hour > 8 and self._hour < 11:
                    return daytime[3]
                if self._hour == 12:
                    return daytime[4]
                if self._hour > 13 and self._hour < 16:
                    return daytime[5]
                if self._hour > 16 and self._hour < 17:
                    return daytime[6]
                if self._hour > 17 and self._hour < 23:
                    return daytime[7]
            if self._month in (3,4,5):
                self._season = seasons[1]
                if self._hour == 0:
                    return daytime[0]
                if self._hour > 0 and self._hour < 5:
                    return daytime[1]
                if self._hour > 5 and self._hour < 6:
                    return daytime[2]
                if self._hour > 6 and self._hour < 11:
                    return daytime[3]
                if self._hour == 12:
                    return daytime[4]
                if self._hour > 13 and self._hour < 18:
                    return daytime[5]
                if self._hour > 18 and self._hour < 19:
                    return daytime[6]
                if self._hour > 19 and self._hour < 23:
                    return daytime[7]
            if self._month in (6,7,8):
                self._season = seasons[2]
                if self._hour == 0:
                    return daytime[0]
                if self._hour > 0 and self._hour < 4:
                    return daytime[1]
                if self._hour > 4 and self._hour < 5:
                    return daytime[2]
                if self._hour > 5 and self._hour < 11:
                    return daytime[3]
                if self._hour == 12:
                    return daytime[4]
                if self._hour > 13 and self._hour < 20:
                    return daytime[5]
                if self._hour > 20 and self._hour < 21:
                    return daytime[6]
                if self._hour > 21 and self._hour < 23:
                    return daytime[7]
            if self._month in (9,10,11):
                self._season = seasons[3]
                if self._hour == 0:
                    return daytime[0]
                if self._hour > 0 and self._hour < 5:
                    return daytime[1]
                if self._hour > 5 and self._hour < 6:
                    return daytime[2]
                if self._hour > 6 and self._hour < 11:
                    return daytime[3]
                if self._hour == 12:
                    return daytime[4]
                if self._hour > 13 and self._hour < 18:
                    return daytime[5]
                if self._hour > 18 and self._hour < 19:
                    return daytime[6]
                if self._hour > 19 and self._hour < 23:
                    return daytime[7]

        # get weekdays
        def get_weekdays(self):
            if self._month < 3 :
                __m = self._month + 12
                __y = self._year - 1
            else :
                __m = self._month
                __y = self._year
            __weekday = ((__y+__y/4-__y/100+__y/400+(13*__m+8)/5+self._day) % 7) - 1
            self.weekday = weekdays[__weekday]
            return weekdays[__weekday]
        
        #def return_date(self):
        #    return unicode(self.year)+". "+unicode(self.month)+". "+unicode(self.day)
        
        @property
        def season(self):
            return self._season
        
        @property
        def sz(self):
            season = str(seasons[_season])
            return seasons[season]

        @property
        def daytime(self):
            return self.daytime
        
        
        @property
        def dt(self):
            daytime = str(daytimes[daytime])
            return daytimes[daytime]
        
        @property
        def year(self): # day as int
            return self._year
        
        @property
        def yy(self): # minutes as mm.
            year = "0" + str(self._year)
            return year[-2:]
        
        @property
        def month(self): # day as int
            return self._month
        
        @property
        def mn(self): # minutes as mm.
            month = "0" + str(self._month)
            return month[-2:]
            
        @property
        def day(self): # day as int
            return self._day
        
        @property
        def dd(self): # minutes as mm.
            day = "0" + str(self._day)
            return day[-2:]
            
        @property
        def hour(self): # _hour as int
            return self._hour

        @property
        def hh(self): # _hours as HH. I'm not familiar enough with python to know if there's a shorter way to do this
            hour = "0" + str(self._hour)
            return hour[-2:] # Q what is doing this 2?

        @property
        def minute(self): # minute as int
            return self._minute

        @property
        def mm(self): # minutes as mm.
            minute = "0" + str(self._minute)
            return minute[-2:]
        
        @property
        def second(self):
            return self._second

        @property
        def ss(self):
            second = "0" + str(self._second)
            return self.second[-2:]

#def __init__(self , year, month, day, weekday, hour, minute, second, season)   
default clk = Clock(2019, 5, 15, 3, 6, 45, 45, 0)





# label start:
#     while True:
#         "Day: [clk.day] Time: [clk.hh]:[clk.mm] [clk.sz] [clk.dt]"
#         $ clk.addtime(15) # Q   how to able add for ex months? Count? 60x24x31~~44640
#         "Date: [clk.day]/[clk.month]/[clk.year]  Time: [clk.hh]:[clk.mm]"
#         $ clk.addtime(44640) # Here Error why?
#         "Blow mind"
#     return
Iv tryied to make it from default clk
in def daytime give _season = None or [0]
where is the mistake in my thinking
Reply
#17
It's really helpful if you give the full text of the error, which would tell us what line the problem is on. I'm guessing it's line 163, where you have _season instead of self._season, but that same mistake might be elsewhere as well, and I missed it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#18
# my own definition of seasons
SEASONS = {
    'winter': (11, 12, 1, 2, 3),
    'spring': (4, 5),
    'summer': (6, 7, 8),
    'autumn': (9, 10),
    }

# extended with seasons
# currently they are all the same.
DAYTIMES = {
    'winter': {
        'midnight': (23, 1),
        'day': (6, 20),
        },
    'spring': {
        'midnight': (23, 1),
        'day': (6, 20),
        },
    'summer': {
        'midnight': (23, 1),
        'day': (6, 20),
        },
    'autumn': {
        'midnight': (23, 1),
        'day': (6, 20),
        },
}

def overlapping_in_range(value, min_val, max_val): 
    if min_val < max_val:
        return min_val <= value < max_val
    elif min_val > max_val:
        return value >= min_val or value < max_val

def get_season(month):
    for season, months in SEASONS.items():
        if month in months:
            return season

def get_daytime(hour, month):
    # get the current season based on month
    season = get_season(month)
    # addressing DAYTIMES[season] to get the
    # sub-dict.
    for daytime, (mmin, mmax) in DAYTIMES[season].items():
        if overlapping_in_range(hour, mmin, mmax):
            return daytime
    return ''


get_daytime(10, 1)
Output:
'day'
I guess this does, what is should do.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#19
Dear Good people.
I am really appreciete your unbelieveble help.
I will not guess
I dont know where i miss something
I'v been tried implement all
I am trying to get the easiest method of formatting for me from @ property
@property
        def ss(self):
            second = "0" + str(self._second)
            return second[-2:]
It is just look like this
"Day: [clk.dd] Time: [clk.hh]:[clk.mm] "
$ clk.addtime(15) 
"Date: [clk.day]/[clk.month]/[clk.year]  Time: [clk.hour]:[clk.minute]"
$ clk.addtime(44640)
But it is still not working
It is far beyond of my abilities of understanding

Today Error

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 49, in script
    $ clk.addtime(0,15,0) # Q   how to able add for ex months? Count? 60x24x31~~44640
  File "game/script.rpy", line 49, in <module>
    $ clk.addtime(0,15,0) # Q   how to able add for ex months? Count? 60x24x31~~44640
  File "game/ReFlow.rpy", line 80, in addtime
    self._weekday = weekdays[weekdays.index(self._weekday)-6]
ValueError: tuple.index(x): x not in tuple

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 49, in script
    $ clk.addtime(0,15,0) # Q   how to able add for ex months? Count? 60x24x31~~44640
  File "D:\Giereczki\renpy-7.2.2-sdk\renpy\ast.py", line 900, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "D:\Giereczki\renpy-7.2.2-sdk\renpy\python.py", line 1930, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/script.rpy", line 49, in <module>
    $ clk.addtime(0,15,0) # Q   how to able add for ex months? Count? 60x24x31~~44640
  File "game/ReFlow.rpy", line 80, in addtime
    self._weekday = weekdays[weekdays.index(self._weekday)-6]
ValueError: tuple.index(x): x not in tuple

Windows-7-6.1.7601-SP1
Ren'Py 7.2.2.491
Your choice 1.0
Fri May 17 12:08:46 2019
The Implements
init python:
    weekdays = ("mon","tue","wed","thu","fri","sat","sun")

    SEASONS = {
    'winter': (11, 12, 1, 2, 3),
    'spring': (4, 5),
    'summer': (6, 7, 8),
    'autumn': (9, 10),
    }

    DAYTIMES = {
    'winter': {
        'midnight': (0, 1),
        'night': (2, 4),
        'dawn': (5, 6),
        'morning': (7, 11),
        'noon': (12, 13),
        'afternoon': (14, 17),
        'dusk': (18, 19),
        'night': (20, 23),
        },
    'spring': {
        'midnight': (0, 1),
        'night': (2, 4),
        'dawn': (5, 6),
        'morning': (7, 11),
        'noon': (12, 13),
        'afternoon': (14, 17),
        'dusk': (18, 19),
        'night': (20, 23),
        },
    'summer': {
        'midnight': (0, 1),
        'night': (2, 4),
        'dawn': (5, 6),
        'morning': (7, 11),
        'noon': (12, 13),
        'afternoon': (14, 17),
        'dusk': (18, 19),
        'night': (20, 23),
        },
    'autumn': {
        'midnight': (0, 1),
        'night': (2, 4),
        'dawn': (5, 6),
        'morning': (7, 11),
        'noon': (12, 13),
        'afternoon': (14, 17),
        'dusk': (18, 19),
        'night': (20, 23),
        },
    }


    class Clock(object):
        def __init__(self , year, month, day, weekday, hour, minute, second, season, daytime):
            self._year = year
            self._month = month
            self._day = day
            self._weekday = weekday
            self._hour = hour
            self._minute = minute
            self._second = second
            self._season = season
            self._daytime = daytime
            
        def addtime(self , hours , minutes , seconds):
            self._second += seconds 
            self._minute += minutes # add minutes
            self._hour += hours

            self._minute += self._second // 60
            self._hour += self._minute // 60
            self._day += self._hour // 24
            
            self._second = self._second % 60
            self._minute = self._minute % 60
            self._hour = self._hour % 24

            self._weekday = weekdays[weekdays.index(self._weekday)-6]
        
            if self._month in (1,3,5,7,8,10,12):
                if self._day > 31:
                    self._day = 1
                    self._month += 1
            elif self._month in (4,6,9,11):
                if self._day > 30:
                    self._day = 1
                    self._month += 1
        #february in leap year
            else :
                # devide by 4
                if (self._year // 4) == 0 :
                    # devide by 100
                    if (self._year // 100) == 0 :
                        # devide by 400
                        if (self._year // 400) == 0 :
                            #leap year
                            __d = 29
                        else:
                            #normal year
                            __d = 28
                    else:
                        #devide by 4 - leap year
                        __d = 29
                else:
                    #normal year
                    __d = 28

                if self._day > __d:
                    self._day = 1
                    self._month += 1
            if self._month > 12:
                self._month = 1
                self._year += 1

        def overlapping_in_range(value, min_val, max_val): 
            if min_val < max_val:
                return min_val <= value < max_val
            elif min_val > max_val:
                return value >= min_val or value < max_val
 
        def get_season(month):
            for season, months in SEASONS.items():
                if month in months:
                    return season
 
        def get_daytime(hour, month):
            # get the current season based on month
            season = get_season(month)
            # addressing DAYTIMES[season] to get the
            # sub-dict.
            for daytime, (mmin, mmax) in DAYTIMES[season].items():
                if overlapping_in_range(hour, mmin, mmax):
                    return daytime
            return ''
        
        def get_weekdays(self):
            if self._month < 3 :
                __m = self._month + 12
                __y = self._year - 1
            else :
                __m = self._month
                __y = self._year
            __weekday = ((__y+__y/4-__y/100+__y/400+(13*__m+8)/5+self._day) % 7) - 1
            self.weekday = weekdays[__weekday]
            return weekdays[__weekday]

               
        
        @property
        def year(self): # day as int
            return self._year
        
        @property
        def yy(self): # minutes as mm.
            year = "0" + str(self._year)
            return year[-2:]
        
        @property
        def month(self): # day as int
            return self._month
        
        @property
        def mn(self): # minutes as mm.
            month = "0" + str(self._month)
            return month[-2:]
            
        @property
        def day(self): # day as int
            return self._day
        
        @property
        def dd(self): # minutes as mm.
            day = "0" + str(self._day)
            return day[-2:]
            
        @property
        def hour(self): # _hour as int
            return self._hour

        @property
        def hh(self): 
            hour = "0" + str(self._hour)
            return hour[-2:] # Q what is doing this 2?

        @property
        def minute(self): # minute as int
            return self._minute

        @property
        def mm(self): # minutes as mm.
            minute = "0" + str(self._minute)
            return minute[-2:]
        
        @property
        def second(self):
            return self._second

        @property
        def ss(self):
            second = "0" + str(self._second)
            return second[-2:]

#def __init__(self , year, month, day, weekday, hour, minute, second, season)   
default clk = Clock(2019, 5, 15, 3, 6, 45, 45, 0, 1)
The way i v been tried to make it

while True:
        "Day: [clk.dd] Time: [clk.hh]:[clk.mm] "
        $ clk.addtime(0,15,0) 
        "Date: [clk.day]/[clk.month]/[clk.year]  Time: [clk.hour]:[clk.minute]"
        $ clk.addtime(10,0,0) 
        "Blow mind"
    return
I give more than one parameter in $ clk.addtime(15) because there was other error about that we have 4 arg and 2 given and propably minutes given?
So i made 3 and occured this error
I bet i make a misteke in add time method? thats it called method
It is soo frustrating to not know what is going on with ur code


In between my mistakes
I am not sure how exectly work overlapping
Do i need do like this?
DAYTIMES = {
    'winter': {
        'midnight': (0, 1),
        'night': (2, 4),
or i can
DAYTIMES = {
    'winter': {
        'midnight': (0, 1),
        'night': ([b]1[/b], 4),
or even
DAYTIMES = {
    'winter': {
        'midnight': (0,0),
        'night': (0, 4),
I did long dict because it was logical for me
if i am wrong tell me

Who wants to be payed for this help?
I bet i am not soo rich Doh

Sincerely, your nooby, beLIEve


Huh

P.S. In line 146 i v been tried with floor and without self._weekday and self.weekday

Anyway how to use tuple Cry

Obviously i would like to have option to show season but i dont know how

The time worked, showed day and time and after boom error
Reply
#20
For the beginning you could use print-debugging.
Just print the variables before this statement.
self._weekday = weekdays[weekdays.index(self._weekday)-6]
Or put it in a try block and print in the excpetion block the variables.



I try to explain the DAYTIMES a little bit
In [10]: DAYTIMES                                                                                        
Out[10]: 
{'winter': {'midnight': (0, 1),
  'night': (20, 23),
  'dawn': (5, 6),
  'morning': (7, 11),
  'noon': (12, 13),
  'afternoon': (14, 17),
  'dusk': (18, 19)},
 'spring': {'midnight': (0, 1),
  'night': (20, 23),
  'dawn': (5, 6),
  'morning': (7, 11),
  'noon': (12, 13),
  'afternoon': (14, 17),
  'dusk': (18, 19)},
 'summer': {'midnight': (0, 1),
  'night': (20, 23),
  'dawn': (5, 6),
  'morning': (7, 11),
  'noon': (12, 13),
  'afternoon': (14, 17),
  'dusk': (18, 19)},
 'autumn': {'midnight': (0, 1),
  'night': (20, 23),
  'dawn': (5, 6),
  'morning': (7, 11),
  'noon': (12, 13),
  'afternoon': (14, 17),
  'dusk': (18, 19)}}

In [11]: DAYTIMES.keys()                                                                                 
Out[11]: dict_keys(['winter', 'spring', 'summer', 'autumn'])

In [12]: [d.keys() for d in DAYTIMES.values()]                                                           
Out[12]: 
[dict_keys(['midnight', 'night', 'dawn', 'morning', 'noon', 'afternoon', 'dusk']),
 dict_keys(['midnight', 'night', 'dawn', 'morning', 'noon', 'afternoon', 'dusk']),
 dict_keys(['midnight', 'night', 'dawn', 'morning', 'noon', 'afternoon', 'dusk']),
 dict_keys(['midnight', 'night', 'dawn', 'morning', 'noon', 'afternoon', 'dusk'])]

In [13]: month = 5                                                                                       

In [14]: SEASONS[month]                                                                                  
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-14-251ea90b9689> in <module>
----> 1 SEASONS[month]

KeyError: 5

In [15]: for season, months in SEASONS.items(): 
    ...:     # key, value 
    ...:     if month in months: 
    ...:         print(season) 
    ...:         # break out of the loop 
    ...:         # return is not possible on module level 
    ...:         break 
    ...:         # lath season is still there after 
    ...:         # leaving the block 
    ...:                                                                                                 
spring

In [16]: season                                                                                          
Out[16]: 'spring'

In [17]: hour = 6                                                                                        

In [18]: # now the overlapping range function                                                            

In [19]: # @staticmethod 
    ...: def overlapping_in_range(value, min_val, max_val):  
    ...:     if min_val < max_val: 
    ...:         return min_val <= value < max_val 
    ...:     elif min_val > max_val: 
    ...:         return value >= min_val or value < max_val 
    ...: # if you use this in a class, you could put a 
    ...: # @staticmethod on the top 
    ...: # you have already your state attributes in your 
    ...: # class 
    ...:  
    ...:  
    ...:                                                                                                 

In [21]: DAYTIMES[season]                                                                                
Out[21]: 
{'midnight': (0, 1),
 'night': (20, 23),
 'dawn': (5, 6),
 'morning': (7, 11),
 'noon': (12, 13),
 'afternoon': (14, 17),
 'dusk': (18, 19)}

In [22]: # ok same again, nested structures are sometimes anoying                                        

In [23]: for daytime, (start_hour, end_hour) in DAYTIMES[season].items(): 
    ...:     # the items methos returns the key, value 
    ...:     # daytime is the key, 
    ...:     # the tuple (start_hour, end_hour) 
    ...:     # consomes the value, which is a tuple (iterable) 
    ...:     # it's called tuple unpacking and is allowd in 
    ...:     # assigment on the left side and in for loops 
    ...:     # now accessing the variable hour 
    ...:     # and call it with the function overlapping 
    ...:     if overlapping_in_range(hour, start_hour, end_hour): 
    ...:         # if you have not used the tuple, 
    ...:         # (start_hour, end_hour) 
    ...:         # instead: 
    ...:         # for daytime, range_tuple in ... 
    ...:         break # remind normally we would return in a function 
    ...:         # daytime and start_hour, end_hour are still available    
    ...:                                                                                                 

In [24]: daytime                                                                                         
Out[24]: 'dusk'

In [25]: start_hour                                                                                      
Out[25]: 18

In [26]: end_hour                                                                                        
Out[26]: 19

In [27]: foo, (bar, baz) = 1, (2 , 3)                                                                    

In [28]: foo, bar, baz                                                                                   
Out[28]: (1, 2, 3)

In [29]: # or better visible                                                                             

In [30]: (foo, (bar, baz)) = (1, (2, 3)) # I love Python                                                 

In [31]: foo                                                                                             
Out[31]: 1

In [32]: bar                                                                                             
Out[32]: 2

In [33]: baz                                                                                             
Out[33]: 3

In [34]: # the overlapping function works as follows 
    ...: # ----- 
    ...: # min_val == start_hour 
    ...: # max_val == end_hour 
    ...: #  
    ...: # start_hour is inclusive 
    ...: # end_hour is exclusive 
    ...: # mathematically correct expressed: 
    ...: # [start_hour, end_hour) 
    ...: # you have to add the minutes to be able 
    ...: # to get the right result for the maximum 
    ...: # if the end_hour is 00:59 and the is 
    ...: # 01:00, you can just add the minutes to the integer 
    ...: # hour = hour + minute / 60 
    ...: # and when you want to do this also for seconds 
    ...: # hour = hour + minute / 60 + second / 3600 
    ...: # no parenthesis needed, Python follows the rules of Math 
    ...: # hour is after this operation a float, no longer an integer 
    ...: # you can compare integers with floats and reversed 
    ...: # but never compare of equality of floats, won't work.. 
    ...: # 
    ...: # now the details about the function overlapping 
    ...: # 
    ...: # first case, min value is less than max value 
    ...: #  
    ...: # min value is less than or equal hour 
    ...: # max value is less then hour 
    ...: # ---- 
    ...: # second case, min value is bigger than max value 
    ...: #  
    ...: # current_hour is bigger or equal min_val 
    ...: # or, (hour won't be bigger than 23) 
    ...: # current_hour is less than max_val 
    ...: # 
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,224 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 402 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,638 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,244 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,337 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,278 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 912 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  I dont know why my function won't work? MehHz2526 3 1,207 Nov-28-2022, 09:32 PM
Last Post: deanhystad
  Something the code dont work AlexPython 13 2,273 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  Error I don't understand finndude 2 5,118 Oct-12-2022, 02:43 PM
Last Post: finndude

Forum Jump:

User Panel Messages

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