Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'syntax': 0.03; 'test,': 0.05; '0),': 0.09; 'terry': 0.09; 'def': 0.10; 'index': 0.13; "'a',": 0.16; "'c',": 0.16; "'d',": 0.16; '2),': 0.16; 'letter:': 0.16; 'loops': 0.16; 'passed.': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'copied': 0.17; 'jan': 0.18; 'tests': 0.18; 'code,': 0.18; 'to:name:python-list@python.org': 0.20; 'subject:problem': 0.22; 'errors': 0.23; 'header:In-Reply-To:1': 0.25; 'wrote': 0.26; 'assert': 0.29; 'index,': 0.29; 'url:mailman': 0.29; 'url:python': 0.32; 'url:listinfo': 0.32; 'to:addr:python-list': 0.33; 'code:': 0.33; 'version': 0.34; 'pm,': 0.35; 'url:org': 0.36; 'subject:with': 0.36; 'thank': 0.36; 'charset:us-ascii': 0.36; 'well.': 0.37; 'subject:: ': 0.38; 'possible.': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'url:mail': 0.40; 'header:Message-Id:1': 0.62; 'gone': 0.64; 'following.': 0.65; 'news': 0.68; 'soon': 0.70; 'exercise,': 0.84; 'ore': 0.84; 'received:212.52.84': 0.84; 'received:libero.it': 0.84; 'received:212.52': 0.91 X-CTCH-Spam: Unknown X-CTCH-RefID: str=0001.0A0C0208.514C0278.017C,ss=1,re=0.000,fgs=0 X-libjamoibt: 1823 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) Subject: Re: problem with function From: leonardo In-Reply-To: Date: Fri, 22 Mar 2013 08:04:23 +0100 Content-Transfer-Encoding: quoted-printable References: <870147F2-AF34-456A-BDF3-12C0A2F67A69@icloud.com> To: "python-list@python.org" X-Mailer: Apple Mail (2.1499) 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1363935999 news.xs4all.nl 6951 [2001:888:2000:d::a6]:42557 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41684 thank you all Il giorno 22/mar/2013, alle ore 00:20, Terry Reedy ha = scritto: > On 3/21/2013 2:31 PM, leonardo selmi wrote: >=20 >> i wrote the following code: >>=20 >> def find(word, letter): >> index =3D 0 >> while index < len(word): >> if word[index] =3D=3D letter: >> return index >> index =3D index + 1 >> return -1 >=20 > Since this is a learning exercise, consider the following. >=20 > def find(word, letter): > for index, let in enumerate(word): > if let =3D=3D letter: > return index > return -1 >=20 > for w, l, n in (('abc', 'a', 0), ('abc', 'c', 2), ('abc', 'd', -1)): > assert find(w, l) =3D=3D n > print("no news is good news") >=20 > I copied the code, wrote the test, ran it, and it passed. I then = re-wrote until syntax errors were gone and the new version passed. For = loops are specialized, easier-to-write version of while loops that scan = the items of a collection (iterable). Learn them and use them well. = Learn to write automated tests as soon as possible. >=20 > --=20 > Terry Jan Reedy >=20 > --=20 > http://mail.python.org/mailman/listinfo/python-list