Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:error': 0.03; 'value,': 0.04; 'say,': 0.05; 'result,': 0.07; 'strings.': 0.09; 'python': 0.11; 'def': 0.12; '"g"': 0.16; 'discarded.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'given,': 0.16; 'guess.': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'name)': 0.16; 'received:192.168.1.4': 0.16; 'there!': 0.16; 'valid.': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'print': 0.22; 'header:User- Agent:1': 0.23; 'string,': 0.24; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'skip:( 20': 0.30; 'but': 0.35; "didn't": 0.36; 'method': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'name': 0.63; 'skip:n 10': 0.64; 'by:': 0.65; 'surprise': 0.74; 'fail.': 0.84; '800': 0.91; 'subject:Lists': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=VomtAoGn c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=aoAcgTy3oA8A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=hLJTyyUxTMg8-DQDr3cA:9 a=HKi1CmgCqFQQLv4a:21 a=q3V0f8xIYxXI3H7W:21 a=QEXdDO2ut3YA:10 a=quqSHGLc1FYA:10 X-AUTH: mrabarnett:2500 Date: Thu, 23 Oct 2014 22:03:23 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: I am out of trial and error again Lists References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1414098214 news.xs4all.nl 2849 [2001:888:2000:d::a6]:44225 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79549 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)) >