I am getting the following error when running the code below:
prev_suffix = ('_' current_year '_q' str(int(current_quarter) - 2))
8 elif cq == '1':
----> 9 prev_suffix = ('_' current_year -1 '_q' str(int(current_quarter) 2))
TypeError: unsupported operand type(s) for -: 'str' and 'int'
Not entirely sure why this is not working
x = datetime.datetime.now()
current_year = str(x.year)[2:]
current_quarter = str(((x.month-1)//3) 1)
current_suffix = ('_' current_year '_q' current_quarter)
if current_quarter == '3':
prev_suffix = ('_' current_year '_q' str(int(current_quarter) - 2))
elif cq == '1':
prev_suffix = ('_' current_year -1 '_q' str(int(current_quarter) 2))
CodePudding user response:
It is because of
current_year -1
It should be
str(int(current_year) - 1))
This being the final version:
x = datetime.datetime.now()
current_year = str(x.year)[2:]
current_quarter = str(((x.month-1)//3) 1)
current_suffix = ('_' current_year '_q' current_quarter)
if current_quarter == '3':
prev_suffix = ('_' current_year '_q' str(int(current_quarter) - 2))
elif current_quarter == '1':
print(type(current_year), type(current_quarter))
prev_suffix = ('_' str(int(current_year) - 1) '_q' str(int(current_quarter) 2))
CodePudding user response:
Because in line prev_suffix = ('_' current_year '_q' str(int(current_quarter) - 2))
You try to add and subtract from string
current_year type is a string and you try to subtract 1 from current_year -1
You can do this like this
str(int(current_year) -1)
