Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 22nd, 2004, 8:29 PM   #1
Insomniac
Programmer
 
Insomniac's Avatar
 
Join Date: Aug 2004
Location: Cloud #9
Posts: 47
Rep Power: 0 Insomniac is on a distinguished road
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:

Insomniac is offline   Reply With Quote
Old Aug 23rd, 2004, 2:58 AM   #2
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 708
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
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]
wourld weild the results

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!
thechristelegacy is offline   Reply With Quote
Old Aug 23rd, 2004, 3:09 AM   #3
Insomniac
Programmer
 
Insomniac's Avatar
 
Join Date: Aug 2004
Location: Cloud #9
Posts: 47
Rep Power: 0 Insomniac is on a distinguished road
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:

Insomniac is offline   Reply With Quote
Old Aug 23rd, 2004, 3:21 AM   #4
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 708
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
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.
thechristelegacy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC