- My Forums
- Tiger Rant
- LSU Recruiting
- SEC Rant
- Saints Talk
- Pelicans Talk
- More Sports Board
- Fantasy Sports
- Golf Board
- Soccer Board
- O-T Lounge
- Tech Board
- Home/Garden Board
- Outdoor Board
- Health/Fitness Board
- Movie/TV Board
- Book Board
- Music Board
- Political Talk
- Money Talk
- Fark Board
- Gaming Board
- Travel Board
- Food/Drink Board
- Ticket Exchange
- TD Help Board
Customize My Forums- View All Forums
- Show Left Links
- Topic Sort Options
- Trending Topics
- Recent Topics
- Active Topics
Started By
Message
re: Python programming?
Posted on 4/13/15 at 2:10 pm to dandug001
Posted on 4/13/15 at 2:10 pm to dandug001
As somebody said, one way or another you need to make a table of months and days in the months and use that.
If you've gotten to the point in the class where you know about dictionaries, that would be the way to go -i.e. something like (off the top of my head here, don't take this too literally):
months = {1:31,2:28,3:31,4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
then in your loop:
if (day > months [month]) print "gimme a better date"
and loop again. Keep doing that until you get a valid response.
If you don't want to use a dictionary, then since you're getting 'month' as an int anyway, you can just use a list of days per month:
months = [31,28,31,30,31,30,31,31,30,31,30,31]
and do the same thing: if (day > months [month]) ...
If you're still shaky on that, then you'll probably want to just put all that logic into the loop explicitly:
month =
day =
year =
if (month == 1 AND day > 31) { complain }
elif (month == 2 AND day > 28 { complain }
...
if (you complained) { start loop again }
EDIT (I read your second post): yeah, on the one hand, this is probably more than they want if you're looking at really basic operations and mostly non-branching control flow. On the other hand, input validation is way more important than even some professional developers seem to think, and it's never too early to at least think about it. You're ahead of the game here, even if it is overthinking the assignment.
If you've gotten to the point in the class where you know about dictionaries, that would be the way to go -i.e. something like (off the top of my head here, don't take this too literally):
months = {1:31,2:28,3:31,4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}
then in your loop:
if (day > months [month]) print "gimme a better date"
and loop again. Keep doing that until you get a valid response.
If you don't want to use a dictionary, then since you're getting 'month' as an int anyway, you can just use a list of days per month:
months = [31,28,31,30,31,30,31,31,30,31,30,31]
and do the same thing: if (day > months [month]) ...
If you're still shaky on that, then you'll probably want to just put all that logic into the loop explicitly:
month =
day =
year =
if (month == 1 AND day > 31) { complain }
elif (month == 2 AND day > 28 { complain }
...
if (you complained) { start loop again }
EDIT (I read your second post): yeah, on the one hand, this is probably more than they want if you're looking at really basic operations and mostly non-branching control flow. On the other hand, input validation is way more important than even some professional developers seem to think, and it's never too early to at least think about it. You're ahead of the game here, even if it is overthinking the assignment.
This post was edited on 4/13/15 at 2:16 pm
Posted on 4/13/15 at 2:20 pm to tokenBoiler
quote:
On the other hand, input validation is way more important than even some professional developers seem to think, and it's never too early to at least think about it. You're ahead of the game here, even if it is overthinking the assignment.
If this is a college level course that started back in January, then by this point, they should most certainly have to validate the user's input against possible combinations of the month and number of days.
The assignment as listed in the OP seems like it would be a suitable homework assignment for the first week of class - not 3/4 of the way through the semester.
Popular
Back to top
Follow TigerDroppings for LSU Football News