473,545 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

leap year problem

16 New Member
I have another problem hopeful am not bothering you guys too much if u can help that would be wonderful. I have this code here
Expand|Select|Wrap|Line Numbers
  1. def year2():
  2.     print'This program validates days and months of the year'
  3.     day = validate('Enter dayNumber: ',1,31);
  4.     month = validate('Enter monthNumber: ',1,12);
  5.     while(True):
  6.         if (month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12 and day <=31)and(month == 4 or month == 6 or month == 9 or month == 11 and day <=30)and(month ==2 and day <=28):
  7.             break;
  8.         else:
  9.             print day,'of',month,'is an invalid date, and can never happen'
  10.             break;
  11.     if(month==1):month ='January';
  12.     elif(month==2): month ='Febuary';
  13.     elif(month==3): month ='March';
  14.     elif(month==4): month ='April';
  15.     elif(month==5): month = 'May';
  16.     elif(month==6): month ='June';
  17.     elif(month==7): month ='July';
  18.     elif(month==8): month ='August';
  19.     elif(month==9): month ='September';
  20.     elif(month==10): month ='October';
  21.     elif(month==11): month ='November';
  22.     elif(month==12): month ='December';
  23.     print day,'of',month,'is a valid date'
  24.  
  25. def validate(prompt,low,hi):
  26.     while(True):
  27.         num = input(prompt)
  28.         if num >= low and num <= hi:
  29.             return num
  30.         else:
  31.             print num, " - error"
  32.  
basically the code is meant to be calculating the days and months of the year so if the user inputs a wrong date for example 32nd of March or 30 of Feb a message will be outputed tom let them know that it is not a valid date.
The problem is when run and enter the correct date i get two messages like this below

>>> year2()
This program validates days and months of the year
Enter dayNumber: 1
Enter monthNumber: 3
1 of 3 is an invalid date, and can never happen
1 of March is a valid date
>>>

how do if fix this because 1st of March is valid date and am getting two messages here. The other problem is something i dont have an idea of how to go about it. I have to add an extra parameter year and the full date validated, if the year is divisible by 4 i have to assume that it is a leap year. so this means that in a leap year 29th of Feb will be a valid date and in a non leap year it wont.
Thanks guys
May 29 '07 #1
5 3677
ghostdog74
511 Recognized Expert Contributor
I have another problem hopeful am not bothering you guys too much if u can help that would be wonderful. I have this code here
Expand|Select|Wrap|Line Numbers
  1. def year2():
  2.     print'This program validates days and months of the year'
  3.     day = validate('Enter dayNumber: ',1,31);
  4.     month = validate('Enter monthNumber: ',1,12);
  5.     while(True):
  6.         if (month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12 and day <=31)and(month == 4 or month == 6 or month == 9 or month == 11 and day <=30)and(month ==2 and day <=28):
  7.             break;
  8.         else:
  9.             print day,'of',month,'is an invalid date, and can never happen'
  10.             break;
  11.     if(month==1):month ='January';
  12.     elif(month==2): month ='Febuary';
  13.     elif(month==3): month ='March';
  14.     elif(month==4): month ='April';
  15.     elif(month==5): month = 'May';
  16.     elif(month==6): month ='June';
  17.     elif(month==7): month ='July';
  18.     elif(month==8): month ='August';
  19.     elif(month==9): month ='September';
  20.     elif(month==10): month ='October';
  21.     elif(month==11): month ='November';
  22.     elif(month==12): month ='December';
  23.     print day,'of',month,'is a valid date'
  24.  
  25. def validate(prompt,low,hi):
  26.     while(True):
  27.         num = input(prompt)
  28.         if num >= low and num <= hi:
  29.             return num
  30.         else:
  31.             print num, " - error"
  32.  
basically the code is meant to be calculating the days and months of the year so if the user inputs a wrong date for example 32nd of March or 30 of Feb a message will be outputed tom let them know that it is not a valid date.
The problem is when run and enter the correct date i get two messages like this below

>>> year2()
This program validates days and months of the year
Enter dayNumber: 1
Enter monthNumber: 3
1 of 3 is an invalid date, and can never happen
1 of March is a valid date
>>>

how do if fix this because 1st of March is valid date and am getting two messages here. The other problem is something i dont have an idea of how to go about it. I have to add an extra parameter year and the full date validated, if the year is divisible by 4 i have to assume that it is a leap year. so this means that in a leap year 29th of Feb will be a valid date and in a non leap year it wont.
Thanks guys
this is a shorter way to validate your date, making use of the time strptime() method.
Expand|Select|Wrap|Line Numbers
  1. import time
  2. year="2007"
  3. month="9"
  4. day="31"
  5. try:
  6.     time.strptime("%s%s%s"%(year,month,day),"%Y%m%d")
  7. except Exception,e:
  8.     print e
  9.  
pls learn to use dictionaries. for what you are doing, you can store your months into dictionaries, for example :
Expand|Select|Wrap|Line Numbers
  1. months = { "1":"Januaro y" , "2":"February" # and so on.}
  2.  
May 29 '07 #2
bartonc
6,596 Recognized Expert Expert
I have another problem hopeful am not bothering you guys too much if u can help that would be wonderful. I have this code here
Expand|Select|Wrap|Line Numbers
  1. def year2():
  2.     print'This program validates days and months of the year'
  3.     day = validate('Enter dayNumber: ',1,31);
  4.     month = validate('Enter monthNumber: ',1,12);
  5.     while(True):
  6.         if (month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12 and day <=31)and(month == 4 or month == 6 or month == 9 or month == 11 and day <=30)and(month ==2 and day <=28):
  7.             break;
  8.         else:
  9.             print day,'of',month,'is an invalid date, and can never happen'
  10.             break;
  11.     if(month==1):month ='January';
  12.     elif(month==2): month ='Febuary';
  13.     elif(month==3): month ='March';
  14.     elif(month==4): month ='April';
  15.     elif(month==5): month = 'May';
  16.     elif(month==6): month ='June';
  17.     elif(month==7): month ='July';
  18.     elif(month==8): month ='August';
  19.     elif(month==9): month ='September';
  20.     elif(month==10): month ='October';
  21.     elif(month==11): month ='November';
  22.     elif(month==12): month ='December';
  23.     print day,'of',month,'is a valid date'
  24.  
  25. def validate(prompt,low,hi):
  26.     while(True):
  27.         num = input(prompt)
  28.         if num >= low and num <= hi:
  29.             return num
  30.         else:
  31.             print num, " - error"
  32.  
basically the code is meant to be calculating the days and months of the year so if the user inputs a wrong date for example 32nd of March or 30 of Feb a message will be outputed tom let them know that it is not a valid date.
The problem is when run and enter the correct date i get two messages like this below

>>> year2()
This program validates days and months of the year
Enter dayNumber: 1
Enter monthNumber: 3
1 of 3 is an invalid date, and can never happen
1 of March is a valid date
>>>

how do if fix this because 1st of March is valid date and am getting two messages here. The other problem is something i dont have an idea of how to go about it. I have to add an extra parameter year and the full date validated, if the year is divisible by 4 i have to assume that it is a leap year. so this means that in a leap year 29th of Feb will be a valid date and in a non leap year it wont.
Thanks guys
My friend ghostdog74 is the best I know at using the python library for everything from basic to advanced programming. There are many ways to get and validate dates (he has omitted the use of the calendar module). And I really like his use of a dictionary for defining the relationship between 1-12:"January"-"December". But I think that you are on the right track for learning basic python.

The only bother is that I am the only one who can add [ code ] tags to your post. You need to learn to do this so that others can see the structure of you code. Instructions are on the right hand side while you are posting: in GUIDELINES, the third item "Use CODE tags".

For the 1st part of your question, I have straightened out your logic. I hope that you will be able to see the changes and that they make sense to you.
Expand|Select|Wrap|Line Numbers
  1. def year2():
  2.     print'This program validates days and months of the year'
  3.     day = validate('Enter dayNumber: ', 1, 31);
  4.     month = validate('Enter monthNumber: ', 1, 12);
  5.     if ((month == 1 or month == 3 or month == 5 or month == 7\
  6.         or month == 8 or month == 10 or month == 12) and day <= 31)\
  7.         or ((month == 4 or month == 6 or month == 9 or month == 11) and day <= 30)\
  8.         or (month == 2 and day <= 28):
  9.  
  10.         if  (month == 1): month ='January';
  11.         elif(month == 2): month ='Febuary';
  12.         elif(month == 3): month ='March';
  13.         elif(month == 4): month ='April';
  14.         elif(month == 5): month = 'May';
  15.         elif(month == 6): month ='June';
  16.         elif(month == 7): month ='July';
  17.         elif(month == 8): month ='August';
  18.         elif(month == 9): month ='September';
  19.         elif(month == 10): month ='October';
  20.         elif(month == 11): month ='November';
  21.         elif(month == 12): month ='December';
  22.         print day,'of',month,'is a valid date'
  23.     else:
  24.         print day,'of',month,'is an invalid date, and can never happen'
  25.  
  26. def validate(prompt, low, hi):
  27.     while(True):
  28.         num = input(prompt)
  29.         if num >= low and num <= hi:
  30.             return num
  31.         else:
  32.             print num, " - error"
  33.  
  34.  
  35. year2()
  36.  
For the second part, use the modulo fuction realizing that zero and False are essentially the same thing:

>>> 1963 % 4
3
>>> 1964 % 4
0
>>>
Returns False if it IS a leap year.

Hope that helps.
May 29 '07 #3
bvdet
2,851 Recognized Expert Moderator Specialist
I agree with ghostdog's and barton's comments. A dictionary is the best way to go. Here's a little script that may help you:
Expand|Select|Wrap|Line Numbers
  1. monthList = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
  2. daysList = [31,28,31,30,31,30,31,31,30,31,30,31]
  3.  
  4. dateDict = dict(zip(range(1,13), (zip(monthList, daysList))))
  5.  
  6. dateStr = raw_input('Enter month/day/year (xx/xx/xxxx)')
  7. month,day,year = [int(s) for s in dateStr.split('/')]
  8.  
  9. try:
  10.     if year % 4 != 0:
  11.         if day <= dateDict[month][1] and day > 0:
  12.             print '%s %d, %d is a valid date' % (dateDict[month][0], day, year)
  13.         else:
  14.             print '%s %d, %d is NOT a valid date' % (dateDict[month][0], day, year)
  15.     else:
  16.         # Add your conditional code here
  17.         # print "It's LEAP YEAR"
  18.         pass
  19. except:
  20.     print 'You have entered an invalid date.'
May 29 '07 #4
Kasrav
16 New Member
thanks man jus wonderin d u guys get paid for this?
Jun 1 '07 #5
bartonc
6,596 Recognized Expert Expert
thanks man jus wonderin d u guys get paid for this?
Nope. Do this out of the goodness of our hearts.

The Python community has been very good to me (lots of published work - not to mention all the tools- all free, to be dropped into projects that pay). So this is my chance to give back. Who knows when one person that I help will become the next Pythoneer who gets the big bucks and can contribute monitarily to the community???
Jun 1 '07 #6

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

Similar topics

22
4403
by: deepak.rathore | last post by:
can someone give the regular expr. to validate leap yr like 02/29/2000,02/29/00 Thanks
2
5011
by: owz | last post by:
Ok, I am making a program (java class file) 2 work out if a date entered is valid or invalid for the day, month year, and for leap years.. dd/mm/yyyy . I seem 2 have gotten it 2 validate the year and month, but my code errors on the day validation. Error(91,38): variable daysInMonth might not have been initialized. Any thoughts, all help...
8
2956
by: rn216_ccc | last post by:
Hi there all, I found a web site,. http://www.onlineconversion.com/leapyear.htm, where it can give a list of leap years by within the given range by the user, or the user may enter a year and the program will make the decision if it is a leap year or not. In addition, I kind of understand that there are certain rules to calculating leap...
8
3894
by: SONIQ | last post by:
You can determine whether a year is a leap year by testing if it is divisible by 4. However, years that are also divisible by 100 are not leap years, unless they are also divisible by 400, in which case they are leap years. Write a script that allows a user to enter a year and then determine whether the year entered is a leap year. Include a...
12
7656
by: Brigitte Behrmann | last post by:
Can anyone assist with my code. I just cannot get this to work: I have to write a script that allows the user to enter a year & then determine whether it is a leap year or not. The request is for a form with a single text box and using an alert dialog box stating if the entered year is a standard or leap year. Code so far: <HTML> <HEAD>...
37
14904
by: mazwolfe | last post by:
I'm new here, so excuse me if my style is incorrect. Can anyone come up with a better method for this calculation? Code: int is_leap(int year) { switch (year % 19) { case 0: case 3: case 6: case 8: case 11: case 14: case 17: return 1; default: return 0;
5
3477
by: jimix | last post by:
here's what i have. using microsoft studio #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main( void ) { int year, b, c, e; b = 4; c = 100;
4
9061
by: jrw133 | last post by:
Hello. So im a little bit stuck on one of my homework questions. heres the question: Write a C Shell Script that performs the following functions. Asks the user to input a year. The script should then calculate whether or not the year is a leap year. The script should caculate whether or not it is a leap year using the following algorithm. ...
4
4412
by: hallsers | last post by:
Here is the problem i am having: - When a user logs in, on page load it will take the dob of all users in a stored friends list and compare them against the following conditions - To display any friends with a birthday inside the next 3 days, taking into account leap years for some1 born on the 29th of feb to be born on 28th in non leap years...
0
7486
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
7676
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
7932
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7442
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5347
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3473
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
3456
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1905
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
729
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.