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
#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


Messages In This Thread
Clock\Calendar for my game [Renpy] - by beLIEve - May-10-2019, 08:52 AM
Get syntax error. Dont understand why - by beLIEve - May-13-2019, 12:46 AM
RE: Get syntax error. Dont understand why - by DeaD_EyE - May-17-2019, 09:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  World Clock syntax error OscarBoots 1 156 May-03-2024, 05:20 AM
Last Post: snippsat
  Syntax error for "root = Tk()" dlwaddel 15 1,284 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 433 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,685 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,269 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,365 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,319 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 935 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  I dont know why my function won't work? MehHz2526 3 1,216 Nov-28-2022, 09:32 PM
Last Post: deanhystad
  Something the code dont work AlexPython 13 2,316 Oct-17-2022, 08:34 PM
Last Post: AlexPython

Forum Jump:

User Panel Messages

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