|
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 -
def year2():
-
print'This program validates days and months of the year'
-
day = validate('Enter dayNumber: ',1,31);
-
month = validate('Enter monthNumber: ',1,12);
-
while(True):
-
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):
-
break;
-
else:
-
print day,'of',month,'is an invalid date, and can never happen'
-
break;
-
if(month==1):month ='January';
-
elif(month==2): month ='Febuary';
-
elif(month==3): month ='March';
-
elif(month==4): month ='April';
-
elif(month==5): month = 'May';
-
elif(month==6): month ='June';
-
elif(month==7): month ='July';
-
elif(month==8): month ='August';
-
elif(month==9): month ='September';
-
elif(month==10): month ='October';
-
elif(month==11): month ='November';
-
elif(month==12): month ='December';
-
print day,'of',month,'is a valid date'
-
-
def validate(prompt,low,hi):
-
while(True):
-
num = input(prompt)
-
if num >= low and num <= hi:
-
return num
-
else:
-
print num, " - error"
-
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
| |
Share:
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 -
def year2():
-
print'This program validates days and months of the year'
-
day = validate('Enter dayNumber: ',1,31);
-
month = validate('Enter monthNumber: ',1,12);
-
while(True):
-
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):
-
break;
-
else:
-
print day,'of',month,'is an invalid date, and can never happen'
-
break;
-
if(month==1):month ='January';
-
elif(month==2): month ='Febuary';
-
elif(month==3): month ='March';
-
elif(month==4): month ='April';
-
elif(month==5): month = 'May';
-
elif(month==6): month ='June';
-
elif(month==7): month ='July';
-
elif(month==8): month ='August';
-
elif(month==9): month ='September';
-
elif(month==10): month ='October';
-
elif(month==11): month ='November';
-
elif(month==12): month ='December';
-
print day,'of',month,'is a valid date'
-
-
def validate(prompt,low,hi):
-
while(True):
-
num = input(prompt)
-
if num >= low and num <= hi:
-
return num
-
else:
-
print num, " - error"
-
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. -
import time
-
year="2007"
-
month="9"
-
day="31"
-
try:
-
time.strptime("%s%s%s"%(year,month,day),"%Y%m%d")
-
except Exception,e:
-
print e
-
pls learn to use dictionaries. for what you are doing, you can store your months into dictionaries, for example : -
months = { "1":"Januaro y" , "2":"February" # and so on.}
-
| | 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 -
def year2():
-
print'This program validates days and months of the year'
-
day = validate('Enter dayNumber: ',1,31);
-
month = validate('Enter monthNumber: ',1,12);
-
while(True):
-
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):
-
break;
-
else:
-
print day,'of',month,'is an invalid date, and can never happen'
-
break;
-
if(month==1):month ='January';
-
elif(month==2): month ='Febuary';
-
elif(month==3): month ='March';
-
elif(month==4): month ='April';
-
elif(month==5): month = 'May';
-
elif(month==6): month ='June';
-
elif(month==7): month ='July';
-
elif(month==8): month ='August';
-
elif(month==9): month ='September';
-
elif(month==10): month ='October';
-
elif(month==11): month ='November';
-
elif(month==12): month ='December';
-
print day,'of',month,'is a valid date'
-
-
def validate(prompt,low,hi):
-
while(True):
-
num = input(prompt)
-
if num >= low and num <= hi:
-
return num
-
else:
-
print num, " - error"
-
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. - def year2():
-
print'This program validates days and months of the year'
-
day = validate('Enter dayNumber: ', 1, 31);
-
month = validate('Enter monthNumber: ', 1, 12);
-
if ((month == 1 or month == 3 or month == 5 or month == 7\
-
or month == 8 or month == 10 or month == 12) and day <= 31)\
-
or ((month == 4 or month == 6 or month == 9 or month == 11) and day <= 30)\
-
or (month == 2 and day <= 28):
-
-
if (month == 1): month ='January';
-
elif(month == 2): month ='Febuary';
-
elif(month == 3): month ='March';
-
elif(month == 4): month ='April';
-
elif(month == 5): month = 'May';
-
elif(month == 6): month ='June';
-
elif(month == 7): month ='July';
-
elif(month == 8): month ='August';
-
elif(month == 9): month ='September';
-
elif(month == 10): month ='October';
-
elif(month == 11): month ='November';
-
elif(month == 12): month ='December';
-
print day,'of',month,'is a valid date'
-
else:
-
print day,'of',month,'is an invalid date, and can never happen'
-
-
def validate(prompt, low, hi):
-
while(True):
-
num = input(prompt)
-
if num >= low and num <= hi:
-
return num
-
else:
-
print num, " - error"
-
-
-
year2()
-
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.
| | 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: - monthList = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
-
daysList = [31,28,31,30,31,30,31,31,30,31,30,31]
-
-
dateDict = dict(zip(range(1,13), (zip(monthList, daysList))))
-
-
dateStr = raw_input('Enter month/day/year (xx/xx/xxxx)')
-
month,day,year = [int(s) for s in dateStr.split('/')]
-
-
try:
-
if year % 4 != 0:
-
if day <= dateDict[month][1] and day > 0:
-
print '%s %d, %d is a valid date' % (dateDict[month][0], day, year)
-
else:
-
print '%s %d, %d is NOT a valid date' % (dateDict[month][0], day, year)
-
else:
-
# Add your conditional code here
-
# print "It's LEAP YEAR"
-
pass
-
except:
-
print 'You have entered an invalid date.'
| | |
thanks man jus wonderin d u guys get paid for this?
| | 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???
| | Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
22 posts
views
Thread by deepak.rathore@gmail.com |
last post: by
| |
8 posts
views
Thread by rn216_ccc@comcast.net |
last post: by
| | |
37 posts
views
Thread by mazwolfe@gmail.com |
last post: by
| | | | | | | | | | | | | |