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
#21
Hello!
Soo i made sloooooow progress.
I need help how to get the key names from dicts
Like to show season or daytime
Here is actual code
It is working
With weekdays and leap year
init python: weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] SEASONS = { 'winter': (11, 12, 1, 2, 3), 'spring': (4, 5), 'summer': (6, 7, 8), 'autumn': (9, 10), } DAYTIMES = { 'winter': { 'midnight': (0, 1), 'night': (1, 7), 'dawn': (7, 8), 'morning': (8, 11), 'noon': (12, 13), 'afternoon': (14, 17), 'dusk': (18, 19), 'night': (20, 23), }, 'spring': { 'midnight': (0, 1), 'night': (2, 6), 'dawn': (6, 7), 'morning': (7, 11), 'noon': (12, 13), 'afternoon': (14, 18), 'dusk': (19, 20), '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, hour, minute, second, weekday, season): self._year = year self._month = month self._day = day self._hour = hour self._minute = minute self._second = second self._weekday = weekday self._season = season def addtime(self , hours , minutes , seconds, ): self._second += seconds #Adding time segment self._minute += minutes self._hour += hours self._minute += self._second // 60 self._hour += self._minute // 60 self._day += self._hour // 24 if self._month in (1,3,5,7,8,10,12): # Limit day segment 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 else : if (self._year % 4) == 0 and (self._year % 100) != 0 or (self._year % 400) == 0: __d = 29 else: __d = 28 if self._day > __d: self._day = 1 self._month += 1 if self._month > 12: self._month = 1 self._year += 1 self._second = self._second % 60 self._minute = self._minute % 60 self._hour = self._hour % 24 if self._month < 3 : # Week day segment __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] @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 def get_season(month): for season, months in SEASONS.items(): if month in months: return SEASONS.key() 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 '' @property #properting for easy use def wk(self): # [clk.wk] ---> weekday name return self._weekday @property def sz(self): pass @property def dt(self): pass @property def yy(self): year = "000" + str(self._year) return year[-4:] @property def mn(self): month = "0" + str(self._month) return month[-2:] @property def dd(self): day = "0" + str(self._day) return day[-2:] @property def hh(self): hour = "0" + str(self._hour) return hour[-2:] @property def mm(self): minute = "0" + str(self._minute) return minute[-2:] @property def ss(self): second = "0" + str(self._second) return second[-2:] #def __init__(self, year, month, day, hour, minute, second) default clk = Clock(2019, 8, 25, 10, 0, 0, " ", 1)
Can somone show me to dig a season name in clk.sz or get a daytimename in clk.dt
Tried many times and get bound method error not a value or key name
Reply
#22
You could use str.zfill() function for all your padding property methods, e.g.
 @property def yy(self): return str(self._year).zfill(4)
found this very helpful overview of String methods
and here“s the zfill() function
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  POST Syntax error amplay 0 1,297 Aug-07-2024, 02:43 PM
Last Post: amplay
  I dont understand on how to create a Bot that can read info on the screen aloud RaySuS777 0 1,022 Jun-19-2024, 10:02 AM
Last Post: RaySuS777
  is this really a syntax error? Skaperen 4 2,510 May-25-2024, 07:31 AM
Last Post: snippsat
  World Clock syntax error OscarBoots 1 1,730 May-03-2024, 05:20 AM
Last Post: snippsat
  Syntax error for "root = Tk()" dlwaddel 15 10,412 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 2,037 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 8,459 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 4,146 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 3,593 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 3,768 Jan-15-2023, 07:49 PM
Last Post: Gribouillis

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.