Hello good people of Byte,
I am running into a - TypeError: unsupported operand type(s) for -: 'str' and 'int'
For example when I run: - realdate(['2012', '12, '12])
The error occurs. This is what I have for my code: - 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.
3 1457 Rabbit 12,516
Recognized Expert Moderator MVP
You're trying to do a subtraction on a string and an integer.
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: - 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 - # 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
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.
dwblas 626
Recognized Expert Contributor
Notice the difference between the two variables below - is_a_string="2012"
-
is_an_int = 2012
-
-
print is_a_string, type(is_a_string)
-
print is_an_int, type(is_an_int)
-
print "adding", is_an_int + is_a_string
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 =...
|
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.
|
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")
|
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:
--------------------
|
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
| |
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...
|
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.......
|
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
|
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 ...
|
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:
|
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...
| |
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...
|
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. ...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |