"Christopher Brookes" <chris.klai...@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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to