mocqueanh
June 29th, 2007, 08:19 AM
Example for Indexing
# Print out a date, given year, month, and day as numbers
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_number = int(month)
day_number = int(day)
# Remember to subtract 1 from month and day to get a correct index
month_name = months[month_number-1]
ordinal = day + endings[day_number-1]
print month_name + ' ' + ordinal + ', ' + year
I've started with Python, and just know Indexing in Python. This is the example of Indexing that i've copied from my book: beginnig Python.
Something in this code i dont understand. I dont understand the endings. Can you define for me ? How to automate the day number ? E.g: 17th, 2nd,....... this endings make it, but i cant understand the way of endings does.:o
# Print out a date, given year, month, and day as numbers
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_number = int(month)
day_number = int(day)
# Remember to subtract 1 from month and day to get a correct index
month_name = months[month_number-1]
ordinal = day + endings[day_number-1]
print month_name + ' ' + ordinal + ', ' + year
I've started with Python, and just know Indexing in Python. This is the example of Indexing that i've copied from my book: beginnig Python.
Something in this code i dont understand. I dont understand the endings. Can you define for me ? How to automate the day number ? E.g: 17th, 2nd,....... this endings make it, but i cant understand the way of endings does.:o