[Tutor] Re: Calendar question

John Carmona jeannot18 at hotmail.com
Wed Apr 6 02:05:16 CEST 2005



Roel you wrote:
>

---------------------------------------------------------------------------------------------
A dictionary is a data structure containing keys and values that defines
a mapping from each key to its corresponding value. You define it like this:

 >>> squares = {1: 1, 10: 100, 4: 15, 5: 25}

Or an empty dictionary:

 >>> emptydict = {}

Once defined, you can access individual elements via their keys:

 >>> print squares[4]
15

This way you can also assign values to existing elements:
 >>> squares[4] = 4*4
 >>> print squares[4]
16

Or add new elements:
 >>> squares[6] = 16
 >>> squares[7] = 49

Python raises a KeyError exception if you try to read an element with a
non-existing key:
 >>> print squares[9]
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in -toplevel-
    print squares[9]
KeyError: 9

I used numbers for the keys and the values, but in fact any Python
object can be used as a value. Any immutable object can be used as a
key; for now, just remember that you can use numbers, strings and tuples
as keys.

So what you could do is create a dictionary with the names of the months
as keys and the corresponding numbers as values and use that dictionary
to convert the names in the numbers.
---------------------------------------------------------------------------------------------

Thanks Roel for the reply   (same to Kristian and Alan- I will check that 
website, thanks)

OK below you can see what I have written so far, I am a bit confused when 
you say
<<to convert the names in the numbers>>. At this stage when I run the script 
it is asking me to use a string or a number, and if I remove the line 
converting the string into an integer, it is asking me to use integers.
Thanks
JC

---------------------------------------------------------------------------------------------
import calendar

MonthName = {'January': 1,'February': 2, 'March': 3,'April': 4\
         ,'May': 5,'June': 6,'July': 7,'August': 8,\
       'September': 9,'October': 10,'November': 11,'December': 12}

##By the way do the number have to be under that format??

calendar.setfirstweekday(0)
              ##setfirstweekday change the day to what you want it be
              ## 0=Monday, 6=Sunday.
year = int(raw_input("Enter a year: "))
month = (raw_input("Enter a month: "))
MonthName_int = int(MonthName)


print calendar.prmonth(year,month)


---------------------------------------------------------------------------------------------




More information about the Tutor mailing list