473,549 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

My function running into a TypeError

Yozuru
4 New Member
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
3 1457
Rabbit
12,516 Recognized Expert Moderator MVP
You're trying to do a subtraction on a string and an integer.
Mar 10 '14 #2
bvdet
2,851 Recognized Expert Moderator Specialist
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 Recognized Expert Contributor
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
1738
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 $adb,$bgcl,$bgcd, $free; $query = "SELECT * FROM software_licenses WHERE (sID = '$sID')"; $sth = $adb->prepare($query); if($sth) { $res =...
9
2990
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
3427
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 ActiveXObject("CCUtility.CU_ScheduledTaskQueue")
12
5145
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
1981
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 to get the name of function running currently. Could anybody help me? Thanks. ps. OS is SunOS 5.8
10
1977
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 calling the function (since that is what is raised when trying to call it with an illegal list), but that has the rather undesirable side effect of...
1
2542
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 Validate(MyControl As Control) As Boolean If Len() < 6 Then MsgBox "Password too small" ' Etc..Etc.......
20
2054
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.: --sort of clumsy and discouraged by the docs as far as I read
4
1598
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 (Appendix A at the end of the web page). However it seems the function is not thread-safe! Suppose for a certain Table X, the SURROGATEKEYVALUE ...
2
1800
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 2.6.4:
0
7518
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7446
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7715
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7808
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5087
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.