464,687 Members | 1,186 Online
Need help? Post your question and get tips & solutions from a community of 464,687 IT Pros & Developers. It's quick & easy.

My function running into a TypeError

 P: 4 Hello good people of Byte, I am running into a Expand|Select|Wrap|Line Numbers TypeError: unsupported operand type(s) for -: 'str' and 'int' For example when I run: Expand|Select|Wrap|Line Numbers realdate(['2012', '12, '12]) The error occurs. This is what I have for my code: Expand|Select|Wrap|Line Numbers def leapyear(y): # Auxiliary Function     if (y%400 == 0) or (y%4 == 0): return True     elif (y%100 == 0): return False     else: return False   # Q5a ----------------------   def realdate(date): # Format [y, m, d]     D = ['y', 'm', 'd'] # List for date     Mon_len_non = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # Non-leap     Mon_len_leap = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # Leap-year     s = 0 # Non-leapyear     x = D[2]-1 # Leapyear     for i in range(1, D[1]):    x += Mon_len_non[i]     return True     for i in range(1, D[1]):    x += Mon_len_leap[i]     return True     if D[1] == 0 or D[1] < 0:   return False     if D[2] == 0 or D[2] < 0:   return False     if D[1] > 12:   return False     if D[0] >= 2012:         for i in range(2012, D[0]): s += 365 + leapyear(i)         return True     else:   # D[0] < 2012         for i in range(D[0], 2012): s += 365 + leapyear(i)         return True Many thanks for any clarification and help you can provide to me. Mar 10 '14 #1

Yozuru,

I cannot follow what you are trying to do with your code. Are you validating the year, month and date? First off, year 1900 is not a leap year because 1900%400 leaves a remainder but your function returns True. I believe the following uses the correct logic:
Expand|Select|Wrap|Line Numbers
1. def isLeap(yr):
2.     if not yr % 4 and (yr % 100 or not yr % 400):
3.         return True
4.     return False
Here's how I would approach the code to determine if the year, month and day of month integers were valid:
Define function isValidYMD with arguments y, m, and d `def isValidYMD(y,m,d):`
Define a list of last days of the month for leap year and non-leap year as in
Expand|Select|Wrap|Line Numbers
1.     # not leap year
2.     daysList1 = [31,28,31,30,31,30,31,31,30,31,30,31]
3.     # leap year
4.     daysList2 = [31,29,31,30,31,30,31,31,30,31,30,31]
Check to see if month is between 1 and 12
Expand|Select|Wrap|Line Numbers
1. if 1 <= m <= 12:
If so, assign list of leap year days to a temporary identifier if `isLeap(y)` returns True or assign list of non-leap year days to a temporary identifier if `isLeap(y)` returns False.
If d is between 1 and tempList[m-1], return True.

You are done. You may want to add the statement `return False` otherwise the function will return None if the conditions are not met.

 Expert Mod 2.5K+ P: 2,851 Yozuru, I cannot follow what you are trying to do with your code. Are you validating the year, month and date? First off, year 1900 is not a leap year because 1900%400 leaves a remainder but your function returns True. I believe the following uses the correct logic: Expand|Select|Wrap|Line Numbers def isLeap(yr):     if not yr % 4 and (yr % 100 or not yr % 400):         return True     return False Here's how I would approach the code to determine if the year, month and day of month integers were valid: Define function isValidYMD with arguments y, m, and d `def isValidYMD(y,m,d):` Define a list of last days of the month for leap year and non-leap year as in Expand|Select|Wrap|Line Numbers     # not leap year     daysList1 = [31,28,31,30,31,30,31,31,30,31,30,31]     # leap year     daysList2 = [31,29,31,30,31,30,31,31,30,31,30,31] Check to see if month is between 1 and 12 Expand|Select|Wrap|Line Numbers if 1 <= m <= 12: If so, assign list of leap year days to a temporary identifier if `isLeap(y)` returns True or assign list of non-leap year days to a temporary identifier if `isLeap(y)` returns False. If d is between 1 and tempList[m-1], return True. You are done. You may want to add the statement `return False` otherwise the function will return None if the conditions are not met. Mar 10 '14 #3