473,385 Members | 1,641 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.

leap year problem

16
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 3665
ghostdog74
511 Expert 256MB
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 Expert 4TB
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 Expert Mod 2GB
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
thanks man jus wonderin d u guys get paid for this?
Jun 1 '07 #5
bartonc
6,596 Expert 4TB
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
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
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...
8
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...
8
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...
12
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...
37
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:...
5
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
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...
4
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.