I am out of trial and error again Lists
MRAB
python at mrabarnett.plus.com
Thu Oct 23 17:03:23 EDT 2014
On 2014-10-23 21:44, Seymore4Head wrote:
[snip]
>
> I tried to make range(10) work in Python 3 by:
> if int(y) in range(10):
> name.append(str(y))
>
> It doesn't.
>
You didn't say what happened when you tried, but I can guess.
When given, say, "g", it complains that it's not valid. Well, "g" isn't
a valid digit, so no surprise there!
> def nametonumber(name):
> lst=[]
> for y in (name):
> y.lower()
The .lower method _returns_ its result, which is just discarded.
What you need is:
y = y.lower()
> if int(y) in range(10):
Non-digits don't have a numeric value, so that can fail.
> name.append(str(y))
y is already a string, so str(y) is pointless.
> if y in " -()":
> name.append(y)
> if y in "abc":
> name.append("2")
> if y in "def":
> name.append("3")
> if y in "ghi":
> name.append("4")
> if y in "jkl":
> name.append("5")
> if y in "mno":
> name.append("6")
> if y in "pqrs":
> name.append("7")
> if y in "tuv":
> name.append("8")
> if y in "wxyz":
> name.append("9")
> number="".join(str(e) for e in name)
The elements of the list name are already strings.
> return (number)
> a="1-800-getcharter"
> print (nametonumber(a))#1800 438 2427 837
> a="1-800-leo laporte"
> print (nametonumber(a))
> a="1 800 callaprogrammer"
> print (nametonumber(a))
>
More information about the Python-list
mailing list