Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41684

Re: problem with function

Subject Re: problem with function
From leonardo <tampucciolina@libero.it>
Date 2013-03-22 08:04 +0100
References <870147F2-AF34-456A-BDF3-12C0A2F67A69@icloud.com> <kig4ko$hus$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.3610.1363935999.2939.python-list@python.org> (permalink)

Show all headers | View raw


thank you all



Il giorno 22/mar/2013, alle ore 00:20, Terry Reedy <tjreedy@udel.edu> ha scritto:

> On 3/21/2013 2:31 PM, leonardo selmi wrote:
> 
>> i wrote the following code:
>> 
>> def find(word, letter):
>>     index = 0
>>     while index < len(word):
>>         if word[index] == letter:
>>             return index
>>         index = index + 1
>>     return -1
> 
> Since this is a learning exercise, consider the following.
> 
> def find(word, letter):
>    for index, let in enumerate(word):
>        if let  == letter:
>            return index
>    return -1
> 
> for w, l, n in (('abc', 'a', 0), ('abc', 'c', 2), ('abc', 'd', -1)):
>    assert find(w, l)  == n
> print("no news is good news")
> 
> 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.
> 
> -- 
> Terry Jan Reedy
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: problem with function leonardo <tampucciolina@libero.it> - 2013-03-22 08:04 +0100

csiph-web