0

I am trying to def a function which will return the current quarter of the year in a string.

import datetime
date = datetime.datetime(2022,1,1)
        
month = date.month
        
year = date.year
    
if month ==1 or 2 or 3:
    quarter = 'Q1'
if month ==4 or 5 or 6:
    quarter = 'Q2'        
if month ==7 or 8 or 9:
    quarter = 'Q3'
if month ==10 or 11 or 12:
    quarter = 'Q4'          
    
lookup = quarter +str(year)
    
print(lookup)

when I run this function I get Q42022. Am I missing something in the if/or statements.

Any help would be greatly appreciated.

mhooper
  • 89
  • 6

2 Answers2

0

You have to explicitly specify

if month==1 or month==2 or month==3:

You could also check inclusion

if month in (1, 2, 3):
ddg
  • 1,090
  • 1
  • 6
  • 17
0
if month in {1, 2, 3}:
# and so on
dlask
  • 8,776
  • 1
  • 26
  • 30