473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

My function running into a TypeError

Yozuru
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.

3 1443
Rabbit
12,516 Expert Mod 8TB
You're trying to do a subtraction on a string and an integer.
Mar 10 '14 #2
bvdet
2,851 Expert Mod 2GB
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
dwblas
626 Expert 512MB
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

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

Similar topics

5
by: Martoni | last post by:
Can anyone see what's wrong with this function? Running the query directly in MySQL produces correct results (ie a value for free and total_entitlement). function Count_licenses($sID) { global...
9
by: drhowarddrfinedrhoward | last post by:
I see a number of pages with functions like MM_somefunction(). Where does the MM_ come from? I don't see it in any books I'm studying.
1
by: Sipho | last post by:
Hi, I need to instantiate a VB6 function and call it from javascript in ASP. <script language='javascript'> function onScheduleTask() { var ocuScheduler ocuScheduler = new...
12
by: windandwaves | last post by:
Hi Gurus When I have a query in which I use a small function, e.g.: SELECT A03_FILES.ID, A03_FILES.D, hasvt() AS hsvVT FROM A03_FILES; where HasVT is defined below: --------------------
2
by: Yeounkun, Oh | last post by:
Hello, I want to know the name of function() in program. In "gcc", I know that __FUNCTION__ macro is used for getting that. but in "cc", __FUNCTION__ macro does not exist. I can't find function...
10
by: Fredrik Tolf | last post by:
If I have a variable which points to a function, can I check if certain argument list matches what the function wants before or when calling it? Currently, I'm trying to catch a TypeError when...
1
by: ApexData | last post by:
Hello I have an UNBOUND textbox Text1 that I want to validate in code. I want to lock the user into the field until Valid Data is entered. I created the following Function: Private Function...
20
by: elderic | last post by:
Hi there, are there other ways than the ones below to check for <type 'function'> in a python script? (partly inspired by wrapping Tkinter :P) def f(): print "This is f(). Godspeed!" 1.:...
4
by: Larry | last post by:
On the following page: http://www-128.ibm.com/developerworks/db2/library/techarticle/dm-0407zhang/index.html IBM discusses a surrogate key generation function, along with a listing in Java...
2
360monkey
by: 360monkey | last post by:
I recently tried to start learning GUI for python, and reverted back to python 2.6.4 my question: in python 3.1.1,: >>>import title >>>title.class.function() >>>running program in python...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.