By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

My function running into a TypeError

Yozuru
P: 4
Hello good people of Byte,

I am running into a
Expand|Select|Wrap|Line Numbers
  1. TypeError: unsupported operand type(s) for -: 'str' and 'int'
For example when I run:
Expand|Select|Wrap|Line Numbers
  1. realdate(['2012', '12, '12])
The error occurs. This is what I have for my code:

Expand|Select|Wrap|Line Numbers
  1. def leapyear(y): # Auxiliary Function
  2.     if (y%400 == 0) or (y%4 == 0): return True
  3.     elif (y%100 == 0): return False
  4.     else: return False
  5.  
  6. # Q5a ----------------------
  7.  
  8. def realdate(date): # Format [y, m, d]
  9.     D = ['y', 'm', 'd'] # List for date
  10.     Mon_len_non = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # Non-leap
  11.     Mon_len_leap = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] # Leap-year
  12.     s = 0 # Non-leapyear
  13.     x = D[2]-1 # Leapyear
  14.     for i in range(1, D[1]):    x += Mon_len_non[i]
  15.     return True
  16.     for i in range(1, D[1]):    x += Mon_len_leap[i]
  17.     return True
  18.     if D[1] == 0 or D[1] < 0:   return False
  19.     if D[2] == 0 or D[2] < 0:   return False
  20.     if D[1] > 12:   return False
  21.     if D[0] >= 2012:
  22.         for i in range(2012, D[0]): s += 365 + leapyear(i)
  23.         return True
  24.     else:   # D[0] < 2012
  25.         for i in range(D[0], 2012): s += 365 + leapyear(i)
  26.         return True
Many thanks for any clarification and help you can provide to me.
Mar 10 '14 #1

✓ answered by bvdet

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.

Share this Question
Share on Google+
3 Replies


Rabbit
Expert Mod 10K+
P: 12,349
You're trying to do a subtraction on a string and an integer.
Mar 10 '14 #2

bvdet
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
  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.
Mar 10 '14 #3

Expert 100+
P: 614
Notice the difference between the two variables below
Expand|Select|Wrap|Line Numbers
  1. is_a_string="2012"
  2. is_an_int = 2012
  3.  
  4. print is_a_string, type(is_a_string)
  5. print is_an_int, type(is_an_int)
  6. print "adding", is_an_int + is_a_string 
Mar 11 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.