Re: [Tutor] Re: Calendar question
Thanks Kristian, it is working fine now. I am learning so much with those little exercises, I know they are probably very basics to most of the people of this forum but they are great for a beginner like me. Jacob has sent me a few exercise to do, some look pretty difficult for my level so surely that you will see me back. JC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Re: Calendar question
On Apr 6, 2005 11:58 AM, John Carmona <[EMAIL PROTECTED]> wrote: > When you say to double-up the dictionary do you mean using the following > method: > > Dict = [{1:1,2:4,etc.}, > {3:9,4:16, etc}] You're close, but a list of dicts is overkill here; stick to one big dict, and leave the keys as strings, so you can just grab data from raw_input directly, and do something like this: Months = {'january':1, '1':1, 'february':2, '2':2, 'march':3, '3':3 .} Essentially double the number of keys in the dictionary. It's a bit redundant, but you'll be able to then just grab >>>month = raw_input("Enter a month: ") >>>Months[month] with no string->int conversion; the dict does it all for you, just as it does in your code now. -- Kristian kristian.zoerhoff(AT)gmail.com zoerhoff(AT)freeshell.org ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Re: Calendar question
Kristian you have said: - I can think of 2 ways to accomplish this. 1. Try to convert monthString to an int, use the lookup if that fails. This might be a good way to learn try/except processing. 2. Just double-up your dictionary to include a second set of keys: {'1':1, '2':2 ...} and just use your existing lookup method, unmodified. You wouldn't have to worry about string->int conversion after raw_input this way, so it's cleaner to implement. - When you say to double-up the dictionary do you mean using the following method: Dict = [{1:1,2:4,etc.}, {3:9,4:16, etc}] Thanks JC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Re: Calendar question
On Apr 6, 2005 7:12 AM, John Carmona <[EMAIL PROTECTED]> wrote: > > Now I have got another question raising from this script. Would it be > possible for the person doing the input to use either the months name in > writing and also in number? I can think of 2 ways to accomplish this. 1. Try to convert monthString to an int, use the lookup if that fails. This might be a good way to learn try/except processing. 2. Just double-up your dictionary to include a second set of keys: {'1':1, '2':2 ...} and just use your existing lookup method, unmodified. You wouldn't have to worry about string->int conversion after raw_input this way, so it's cleaner to implement. -- Kristian kristian.zoerhoff(AT)gmail.com zoerhoff(AT)freeshell.org ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] Re: Calendar question
Thanks John for your help. Here is the final script (and it is working) -- 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} calendar.setfirstweekday(0) ##setfirstweekday change the day to what you want it be ## 0=Monday, 6=Sunday. year = int(raw_input("Enter a year: ")) monthString = (raw_input("Enter a month: ")) while monthString not in MonthName: print "Unknown month!" monthString = raw_input('Enter a month: ') month = MonthName[monthString] print calendar.prmonth(year,month) -- ##By the way do the number have to be under that format?? I don't understand the question, sorry.. This was a question for me, by that I meant would 01,02,03 etc format work as well (And it does). Sorry for the confusion. Now I have got another question raising from this script. Would it be possible for the person doing the input to use either the months name in writing and also in number? Thanks again John JC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] Re: Calendar question
Quoting John Carmona <[EMAIL PROTECTED]>: Hi John, Some comments --- > 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} You can actually get away with leaving out the backslashes. ie, if I were writing this code, I would probably write something like this: 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 } (I'm not sure what the preferred convention for whitespace is ... I always leave a space after the comma, but have no whitespace surrounding the colon, because that makes it clearer that the two things either side of the ':' are a linked unit) > ##By the way do the number have to be under that format?? I don't understand the question, sorry.. > 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) Try replacing the lines I have marked with: monthString = raw_input("Enter a month: ") month = MonthName[monthString] You might want to review the tutorial section on dictionaries: http://python.org/doc/2.4.1/tut/node7.html#SECTION00750 If you want more robustness, you could try replacing those two lines with this: monthString = raw_input('Enter a month: ') while monthString not in MonthName: print "Unknown month!" monthString = raw_input('Enter a month: ') month = MonthName[monthString] This will check to see if the user's input is one of the keys in your dictionary, and if not, it will ask again. HTH! -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] Re: Calendar question
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 "", 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 <>. 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) - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Re: Calendar question
John Carmona wrote: > Kristian you wrote: > > - > > This assumes all input as integers; if you want months entered by > name, you'll have to write a conversion routine (hint: use a dict). > - > > > I have been looking for a while about doing a conversion routine (using > a dictionary??), are you saying that I should convert a string (January, > February, etc.) into integers. Could you please give me some light (do > not write the code please but just point me to the right direction if > you can) 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 "", 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. Try to play a bit with dictionaries in IDLE (or your Python shell of choice). If anything's not clear, just give a yell. -- If I have been able to see further, it was only because I stood on the shoulders of giants. -- Isaac Newton Roel Schroeven ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor