[Tutor] Help with converting a string into a integer

bob gailer bgailer at gmail.com
Thu Oct 31 14:02:38 CET 2013


On 10/31/2013 7:52 AM, Carmen Salcedo wrote:
> I'm not able to post it right now. All I did to the previous program i 
> emailed was changed isalpha() to str.isalpha.
That does agree with what you posted or got.

The part of your original program that should print a character already is
     print (str.isalpha()
which raises this exception:

   File "N:\Script2.py", line 37, in phoneTranslator
     print(str.isalpha())
TypeError: descriptor 'isalpha' of 'str' object needs an argument

it should be

print n

Exactly what did you type in response to
     phoneNumber = raw_input ("Please enter the phone number: ")

Note you are not consistent in converting letters:
             elif n == "G" or n == "H" or n == "I":
                 n = "4"
             elif n == "J" or n == "K" or n == "L":
                 n = 5
all the numbers should be characters e.g.
                 n = "5"
etc.

There are many ways to get the desired output.
One is:
   collect the characters in one list, say numberList
   Then use slicing to insert the "-" e.g. numberList[3:3] = "-"
   Then print "".join(numberList)

-- 
Bob Gailer
919-636-4239
Chapel Hill NC

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131031/f6e1b3db/attachment.html>


More information about the Tutor mailing list