Page 1
Page 1
Started By
Message

Python programming?

Posted on 4/11/15 at 9:45 pm
Posted by dandug001
Shreveport
Member since Oct 2011
1578 posts
Posted on 4/11/15 at 9:45 pm
I am taking a class on Python right now. I have an assignment that is suppose to calculate if the date is a "magic date" whereas say the date is 6/10/60, the month and day equal the year. I can calculate the numbers, but not sure how I would go about putting it to where you can tell the program what the limit to how many days there are in a month, IE 28 days for february. Be gentle on me, this is my first programming class. Here is my program so far:

month = int(input('Enter month: '))
day = int(input('Enter day of month: '))
year = int(input('Enter 2 digit year: '))


if month*day==year:
print('The number is Magic')

else:
if month*day!=year:
print('The number is not Magic')

it didn't put my indentions for the if-else commands when I posted, but they are there
Posted by Korkstand
Member since Nov 2003
28709 posts
Posted on 4/11/15 at 10:30 pm to
First off your else doesn't need to test not equal, it can just print the date is not magic.

Second, are you required to validate the date? If so, there is surely a module to do it for you. If you aren't allowed to use modules, then you will have to check them manually, but that's not too hard. Just if month is 2, day has to be less than 30, if month is 4, 6, 9, or 11, day has to be less than 31, and the rest less than 32. And of course all have to be greater than 0.
Posted by shutterspeed
MS Gulf Coast
Member since May 2007
63396 posts
Posted on 4/12/15 at 12:16 am to
Load "*" ,8,1
Posted by dj30
New Orleans
Member since Feb 2006
28726 posts
Posted on 4/12/15 at 2:02 am to
I don't know python , but can you just put a bunch of if/else statements depending on the particular month.

As in only allow values between 1-29 for February.

you would have to add the day string in a loop under all the month conditions.
Posted by THRILLHO
Metry, LA
Member since Apr 2006
49517 posts
Posted on 4/12/15 at 2:36 am to
quote:

but not sure how I would go about putting it to where you can tell the program what the limit to how many days there are in a month, IE 28 days for february.


So someone is entering the date manually? The easiest way would be using a case, but I don't know if Python does those. A bunch of if statements would work fine.

I'm rusty on my C++, but something like this would work (cout sends info to the screen, cin receives information from the user and saves it into a variable, otherwise it looks pretty similar)

cout<< "Enter month"
cin>> month
cout << "Enter day"
cin>> day
cout << "Enter year"
cin >> year
if (month == 1 && day >1 && day < 32)
**check if magic date here**
else if (month == 2 && day >1 && day < 29)
**check if magic date here**

then continue the same way throughout the year. After December, make your final statement an else that simply outputs to the screen that the date is invalid, which would be the case if all of the previous if statements failed.
This post was edited on 4/12/15 at 2:38 am
Posted by Cs
Member since Aug 2008
10471 posts
Posted on 4/13/15 at 1:39 pm to
If you still need assistance with this, I wrote a program in Javascript that provides the desired result. I installed a series of while loops into one of the functions to preclude non-existent combinations of days and months.

Translating it to Python shouldn't be terribly difficult, as most of the code is procedural.

Posted by dandug001
Shreveport
Member since Oct 2011
1578 posts
Posted on 4/13/15 at 1:59 pm to
after going to class today, I was told that I was really over thinking the question and that if they wanted the program to break each month down they would have asked for it. My program that I had was what they were wanting.

Thanks for all the help
Posted by Cs
Member since Aug 2008
10471 posts
Posted on 4/13/15 at 2:02 pm to
What level is this Python course?

College? High school?
Posted by tokenBoiler
Lafayette, Indiana
Member since Aug 2012
4415 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
10471 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.
Posted by dandug001
Shreveport
Member since Oct 2011
1578 posts
Posted on 4/13/15 at 3:02 pm to
this is a college class. Just at chapter 3. this is the 4th week of the class. When I asked about the assignment, the teacher did mention about eventually input validation would be required but not yet.
Posted by Cs
Member since Aug 2008
10471 posts
Posted on 4/13/15 at 3:16 pm to
Interesting. What topics have you covered thus far?

I'm honestly curious as to what the pacing is like for a programming course at the college level.
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