[Tutor] I need help on this program please

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Jul 10 12:07:18 CEST 2013


On 5 July 2013 22:07, Ashley Fowler <afowler2334 at yahoo.com> wrote:
> This is what I have to do :

It would be good to explain what you want help with. Does your program
work? Are you just looking to improve it?

[snip]
>
> def main():
>
>     digits = ["2", "3", "4", "5", "6", "7", "8", "9"]
>
>     numeric_phone=" "
>     phone_number = input('Enter 10-Digit phone number: ')
>     ch= digits[index]

I guess this line gives you an error. It would be good to show the
error if you get one. You probably want to move this line into the
loop after you set the value of index.

>     for ch in phone_number:
>         if ch.isalpha():
>             if ch == 'A' or ch == 'B' or ch == 'C':

It's a bit easier to do:

if ch in 'A', 'B', 'C':

Also what about lower case letters e.g. 'a'?

>               index = 0
>
>             if ch == 'D' or ch == 'E' or ch == 'F':
>                 index = 1
>
>             if ch == 'G' or ch == 'H' or ch == 'I':
>                 index = 2
>
>             if ch == 'J' or ch == 'K' or ch == 'L':
>                 index = 3
>
>             if ch == 'M' or ch == 'N' or ch == 'O':
>                 index = 4
>
>             if ch == 'P' or ch == 'Q' or ch == 'R' or ch == 'S':
>                 index = 5
>
>             if ch == 'T' or ch == 'U' or ch == 'V':
>                 index = 6
>
>             if ch == 'W' or ch == 'X' or ch == 'Y' or ch == 'Z':
>                 index = 7
>
>         numeric_phone= numberic_phone+ch
>
>     print(numeric_phone)
>
> This is what I have so far. Can anyone make suggestions or tell me what I
> need to correct?

My suggestion is to make a dict that maps characters to digits e.g.:

digits = {
    '1': '1', '2': '2', ...,
    'A': '2', 'B': '2', ...
}

Then you can find the correct character with digits[ch] instead of
checking against each letter bunch separately.

If you think that something needs correcting then you should say what
happens when you run the code and why it isn't what you want to
happen.


Oscar


More information about the Tutor mailing list