Python Forum
how to avoid a lot of 'if' statments ?
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to avoid a lot of 'if' statments ?
#10
To make it more universal
class Politician(object):
 
    def __init__ (self, name, damage, health):
        self.name = name
        self.damage = damage
        self.health = health
 
    def attack (self, other_guy):
        other_guy.health -= self.damage
        print('БАБАХ!!')
        print('{} атакує {} !!!'.format(self.name, other_guy.name))
        print('У {} лишилось {} ХП\n'.format(other_guy.name, other_guy.health))
     
politicians_data = (('Ляшко', 15, 100), ('Порошенко', 12, 100), ('Яценюк', 9, 100), ('Тимошенко', 11, 100))
politicians = [Politician(name, damage, health) for name, damage, health in politicians_data]

print('Вас вітає гра "Мортал Комбат: Політична Версія"!\n')
print('Наші політики:\n')
for i, pol in enumerate(politicians, start=1):
  print('{} --> {}'.format(i, pol.name))
print('Виберіть батл який бажаєте побачити\n')
 
 
pol1 = int(input('Хто буде бити ? (Виберіть цифру)\n')) - 1
pol2 = int(input('Кого будуть бити ? (Виберіть цифру\n')) -1 
 
politician1 = politicians[pol1]
politician2 = politicians[pol2]
politician1.attack(politician2)
Output:
Вас вітає гра "Мортал Комбат: Політична Версія"! Наші політики: 1 --> Ляшко 2 --> Порошенко 3 --> Яценюк 4 --> Тимошенко Виберіть батл який бажаєте побачити Хто буде бити ? (Виберіть цифру) 3 Кого будуть бити ? (Виберіть цифру 1 БАБАХ!! Яценюк атакує Ляшко !!! У Ляшко лишилось 91 ХП
Now you can add more politician by just changing politicians_data and nothing else
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
how to avoid a lot of 'if' statments ? - by witch - Aug-27-2018, 11:36 AM
RE: how to avoid a lot of 'if' statments ? - by buran - Aug-27-2018, 12:59 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020