Started By
Message

re: Python programming?

Posted on 4/13/15 at 2:10 pm to
Posted by tokenBoiler
Lafayette, Indiana
Member since Aug 2012
4450 posts
Posted on 4/13/15 at 2:10 pm to
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.
This post was edited on 4/13/15 at 2:16 pm
Posted by Cs
Member since Aug 2008
10487 posts
Posted on 4/13/15 at 2:20 pm to
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.
first pageprev pagePage 1 of 1Next pagelast page
refresh

Back to top
logoFollow TigerDroppings for LSU Football News
Follow us on Twitter, Facebook and Instagram to get the latest updates on LSU Football and Recruiting.

FacebookTwitterInstagram