![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Aug 2004
Location: Cloud #9
Posts: 47
Rep Power: 0
![]() |
Here is an example code:
months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', ] # A list with one ending for each number from 1 to 31 endings = ['st', 'nd', 'rd'] +17 * ['th']\ + ['st', 'nd', 'rd'] +7 * ['th']\ + ['st'] year = raw_input('Year: ') month = raw_input('Month(1-12): ') day = raw_input('Day(1-31): ') month_name = months[int(month)-1] ordinal = day + endings[int(day)-1] print month_name + ' ' + ordinal + ', ' + year Now, this program came out fine due to it being an example in my book. Even when I went to create my own more simplified version, I did fine. But, can you explain how Python or the computer for that matter reads where it says 'endings' and is able to properly assign the correct ending for a specific day? I left this portion out of my personal program because it confused the hell out me and also because the book left this detailed part out. That's number 1, number 2 is -is 'ordinal' simply a variable or a function because the book didn't say. I tried adding 'ordinal' to my program and it wouldn't accept it. Please help, Thank you.
__________________
From an IBM Thinkpad T43 - 14.1" SXGA+ - ATI 64 MB X300 - Sonoma 760 - 2 GB RAM - 80 GB HD 5400 - IBM ABG II - FC3, Ubuntu & XPee DevC++, and Macromedia's - Dreamweaver & Flash Pro and a little Adobe Illustrator & Photoshop Sleep? Sleep is for the weak.:cool: |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Alright, well let's take a look at the structures in this program.
What we have are two list. months and endings. Now what we need to remember is that when we have a list, to call an item off the list we have to call it with an offset of one because the first item starts at 0 instead of 1. e.g. list = ['dog','cat','mouse','bird'] print list[1] print list[0] print list[3] print list[2] cat dog mouse bird With that in mind, this is what months look like. Jan 0, Feb 1, Mar 2, Apr 3, May 4, Jun 5, Jul 6, Aug 7, Sept 8, Oct 9, Noc 10, Dec11 Ok, assuming that the user has already entered the date into the raw_inputs, let's take this apart and see how it works. month_name = months[int(month)-1] What that is doing is first turning your answere which is a string into an interger, and then making a call back to the list month with that interger minus one. So in reality it looks like this. If use choose 7 (july) as your month it turns 7 into an interger and then subtracts one, because if it didn't it would choose August. That should clear up how it pics the month. Now for the fun part, the ending ![]() Let's look at your list endings. endings = ['st', 'nd', 'rd'] +17 * ['th']\ + ['st', 'nd', 'rd'] +7 * ['th']\ + ['st'] What that looks like without the +17 * ['th] and +7 * ['th'] is ['st', 'nd', 'rd','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th',st', 'nd', 'rd','th','th','th','th','th','th','th','st'] And when you see it expanded like that, it works the same as what happens when you call months. It takes a number, changes it to an interger, then subtracts one. And for your second question ordinal is just a variable. That wasn't to clear, but I hope if sheds some light. Feel free to ask more questions if need be. How that helps ya out! ![]() |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Aug 2004
Location: Cloud #9
Posts: 47
Rep Power: 0
![]() |
Oh, wow!
Thank you so much. Gotta love us newbies I just didn't understand the arithmetic in that list.... but after you spelled out, I can see it more clearly. He calculated in advance and realized there would be 17 'th''s, so a + 17*th makes perfect sense, same for the other 7* 'th'... simplified math. I love this logical stuff... only with a little more practice. B) Thanks and I appreciate your help.
__________________
From an IBM Thinkpad T43 - 14.1" SXGA+ - ATI 64 MB X300 - Sonoma 760 - 2 GB RAM - 80 GB HD 5400 - IBM ABG II - FC3, Ubuntu & XPee DevC++, and Macromedia's - Dreamweaver & Flash Pro and a little Adobe Illustrator & Photoshop Sleep? Sleep is for the weak.:cool: |
|
|
|
|
|
#4 |
|
Expert Programmer
|
Yeah anytime. These "shortcuts" are important to understand because they can save you a lot of time if you know how to use them. I'm glad I could help.
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|