By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,630 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,630 IT Pros & Developers. It's quick & easy.

leap year problem

P: 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
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 511
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
Expert 5K+
P: 6,596
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
Expert Mod 2.5K+
P: 2,851
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

P: 16
thanks man jus wonderin d u guys get paid for this?
Jun 1 '07 #5

bartonc
Expert 5K+
P: 6,596
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

Post your reply

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