Problem with a dictionary program....

Alex Martelli aleaxit at yahoo.com
Tue Sep 28 11:42:16 EDT 2004


Ling Lee <janimal at mail.trillegaarden.dk> wrote:

> Hello.

Hi!  I suspect (from your name and address) that English is not your
mother tongue (it ain't mine, either), so I hope you'll appreciate some
suggestions in the following about natural language as well as Python.

> I'm trying to write a small program that lets you put in a number as an
> integer and then it tells you the textuel representation of the number.

"Textual".

> Like if your input is 42, it will say four two.

Ah, "Digit by digit".  The "textual representation of 42" would be
"fortytwo" -- a harder problem than outputting "four two".

> I found out that I have to make a dictionary like this: List = { 1:"one",
> 2:"two" and so on )

Yes, such a dict is one possibility (no "have to", really, since there
ARE quite practicable alternatives), but [a] don't name it List, that's
WAY confusing, and [b] you may consider using as keys the digit strings
rather than the corresponding numbers, e.g '1' instead of 1 and so on,
it might simplify your coding.
Still, let's assume that this thing IS what you have, one way or
another.


> and I have to use the raw_input method to get the number:

You don't HAVE to (you could obtain it in other ways) but, sure, it's
one possibility.  (Maybe you overuse "have to" a bit...?)

> 
> indput : raw_input(" Tell me the number you want to transform to textuel
> representaion")

No doubt you mean to use an equals-sign here, not a colon, since you are
assigning the result of raw_input to your variable 'indput' (VERY
confusing name, again).

> The I have to transform the input to a string
> indput = str(indput)

Not at all, this is a "no-operation", since the result of raw_input IS a
string already.

> so that I can count how many decimals the number has, like 23 has 2 decimals
> and 3000 has 4 decimals.

"digits".  When you say a number has "2 decimals" this would normally be
interpreted as meaning "two digits after the decimal point", something
like "27.12".  But I don't see why you would care, anyway.  Rather, try
checking indput.isdigit() to verify all of the chararacters are digits.

> After I have gotten the lenght of the string, I will write a loop, that goes
> through the dictionary as many times as the lengt of the string, and the
> gives me the corresponding numbers, the numner 21 would go 2 times through
> the loop and give me the output two one

Nah, loop on the string directly, that will give you one character at a
time in order.  No need to worry about lengths, number of times through,
etc.  Just "for c in indput: ...", that's all.

 
> Will one of you be so kind and tell me how I count the lengt of the indput
> number i was thinking on something like input.count[:] but that dosnt
> work...

That would be len(indput), but you don't need it.

> 
>  and how I make the loop.

You do it with a 'for' statement as above, no worry about the length.

 
>  Im trying to understand dictionaries but have gotten a bit stuck...
> 
> Thanks for all replies....

The Tutor list might be very helpful to you.  Happy studying!


Alex



More information about the Python-list mailing list