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 ?
#11
(Aug-27-2018, 12:55 PM)Gribouillis Wrote:
(Aug-27-2018, 12:06 PM)witch Wrote: why does it execute choice 1,2,3 at a same time?
Use a list and write simply
politicians = [ryashko, poroh, yacenyuk, tymoshenko]

pol1 = (int(input('Хто буде бити ? (Виберіть цифру)\n')))
pol2 = (int(input('Кого будуть бити ? (Виберіть цифру\n')))

politicans[pol1 - 1].attack(politicians[pol2 - 1])
This also works, thanks for reply, I appericate it ! any idea on how to loop it, so the game doesnt end after one choice ?
Reply
#12
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')
while True:
    try:
        pol1 = int(input('Хто буде бити ? (Виберіть цифру)\n')) - 1
        pol2 = int(input('Кого будуть бити ? (Виберіть цифру\n')) -1
        politician1 = politicians[pol1]
        politician2 = politicians[pol2]
        politician1.attack(politician2)
    except (ValueError, IndexError):
        print('Invalind input')
        continue
Output:
Вас вітає гра "Мортал Комбат: Політична Версія"! Наші політики: 1 --> Ляшко 2 --> Порошенко 3 --> Яценюк 4 --> Тимошенко Виберіть батл який бажаєте побачити Хто буде бити ? (Виберіть цифру) 1 Кого будуть бити ? (Виберіть цифру a Invalind input Хто буде бити ? (Виберіть цифру) 1 Кого будуть бити ? (Виберіть цифру 3 БАБАХ!! Ляшко атакує Яценюк !!! У Яценюк лишилось 85 ХП Хто буде бити ? (Виберіть цифру) 1 Кого будуть бити ? (Виберіть цифру 6 Invalind input Хто буде бити ? (Виберіть цифру)
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
#13
(Aug-27-2018, 01:32 PM)buran Wrote:
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') while True: try: pol1 = int(input('Хто буде бити ? (Виберіть цифру)\n')) - 1 pol2 = int(input('Кого будуть бити ? (Виберіть цифру\n')) -1 politician1 = politicians[pol1] politician2 = politicians[pol2] politician1.attack(politician2) except (ValueError, IndexError): print('Invalind input') continue
Output:
Вас вітає гра "Мортал Комбат: Політична Версія"! Наші політики: 1 --> Ляшко 2 --> Порошенко 3 --> Яценюк 4 --> Тимошенко Виберіть батл який бажаєте побачити Хто буде бити ? (Виберіть цифру) 1 Кого будуть бити ? (Виберіть цифру a Invalind input Хто буде бити ? (Виберіть цифру) 1 Кого будуть бити ? (Виберіть цифру 3 БАБАХ!! Ляшко атакує Яценюк !!! У Яценюк лишилось 85 ХП Хто буде бити ? (Виберіть цифру) 1 Кого будуть бити ? (Виберіть цифру 6 Invalind input Хто буде бити ? (Виберіть цифру)

thanks, it worked !
Reply


Forum Jump:

User Panel Messages

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