[Tutor] Search function in a list-tuples
ALAN GAULD
alan.gauld at btinternet.com
Fri Feb 25 19:44:43 CET 2011
CCing group. Please use ReplyAll in replies to the list.
> Hi, thank your for your answer. i wrote this now :
>def HeroExist(HerosName):
> for heros in herosAll:
> if HerosName in heros.name:
> herosId = heros.id
> return herosId
> return None
>
>HerosName=input("Enter heros name : ")
>ch = HeroExist(HerosName)
>if ch:
> ch.DisplayCharacterInfos()
>else :
> print ('This heros does\'nt exist')
>
>But whatever i type, he says : heros doesn't exist :/
>
>That suggets a problem with how the name is being stored.
Can you send the code for the Character class, esp[ecially the init method.
Plus a cut n paste of the output of a ruin of the programme so that
we can see exactly what you are inputting and what the
results are?
BTW you don't need the \ in the last print. You are using double quotes
around the string so you can just type the single quote directly
print ( "This hero doesn't exist')
Alan G.
2011/2/25 Alan Gauld <alan.gauld at btinternet.com>
>
>"Christopher Brookes" <chris.klaitos at gmail.com> wrote
>>
>>
>>Hi, is there a better way to do this ? (*heros are Character*)
>>>
>There are several better ways but mainly they involve
>techniques that may be too advanced for you at present,
>so we'll keep it simple for now :-)
>
>
>
>herosAll = [
>>Character(0,"Chris","Soldat fort",type[0],15,5,8,50,1),
>>Character(1,"Antaa","Soldat moins fort",type[0],15,5,8,50,1)]
>>
>>def HeroExist(HerosName):
>> herosId = -1
>> for heros in herosAll:
>> if HerosName in heros.name:
>> herosId = heros.id
>> if herosId != -1:
>> return herosId
>> else:
>> return -1
>>
replace from the for loop down with:
>
>
> for heros in herosAll:
> if HerosName in heros.name:
> herosId = heros.id
>
return herosId
> return None # This allows more convenient testing, see below
>
>Which has the advantage of being slightly faster too.
>
>
>
> HerosName=input("Enter heros name : ")
>> if Character.HeroExist(HerosName) != -1:
>>
HeroExist is not a method of Character ( although you could
>make it so - one of the better solutions I alluded to above! )
>So you don't need to prefix it with Character.
>
>And you can assign the return value to a variable so
>you avoid calling it twice:
>
>
> HerosName=input("Enter heros name : ")
>
ch = HeroExist(HerosName):
> if ch: # because None is treated as False
> ch.DisplayCharacterInfos()
>
> else :
> print ('This heros does\'nt exist')
>
>
HTH,
>
>
>--
>Alan Gauld
>Author of the Learn to Program web site
>http://www.alan-g.me.uk/
>
>
>
>_______________________________________________
>Tutor maillist - Tutor at python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
>--
>Brookes Christopher.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110225/16b59e1e/attachment.html>
More information about the Tutor
mailing list