Problem with a dictionary program....

Dan Perl danperl at rogers.com
Tue Sep 28 12:21:44 EDT 2004


"Alex Martelli" <aleaxit at yahoo.com> wrote in message 
news:1gku3wn.1o7mvyb1gtif84N%aleaxit at yahoo.com...
> Ling Lee <janimal at mail.trillegaarden.dk> wrote:
>   ...
>> List =
>> {1:"one",2:"two",3:"three",4:"four",5:"five",6:"six",7:"seven",8:"eight",9
>> :"nine"}
>
> You're missing 0, so an input such as 103 would give an error even if
> all the rest of your code was correct.

Here's another idea: use a (real) list instead of a dictionary:
List = 
["zero","one","two","three","four","five","six","seven","eight","nine"]

Then you have to convert the 'character' variable with int(character) before 
indexing in List.  Now, this would not help you in learning dictionaries, 
but it must be a good lesson for something.  ;-)

>> output = []
>> for character in indput:
>>   output.append(List[character])
>>   print ', '.join(output)
>
> This will print N times when you have N digits... outdent the print
> statement so it's executed after the 'for' and not inside it.

That print statement is actually wrong in many ways.  It was probably meant 
to be something like
    print ','.join(List[int(character)])    # List[int(character)], not 
output
but even that would start with ',' and it would be on multiple lines 
(probably not your intention).  Alex is right.  You're better off with an 
unindented
    print output
although that would have square brackets around the output list.  Or try an 
indented
    print List[int(character)],
which would just not have the commas between the numbers 





More information about the Python-list mailing list