Как можно улучшить данный код?
from random import choice
class SlotMachine(object):
def __init__(self):
self.items = ['?', '?', '?', '?', '?']
self.top = []
self.middle = []
self.bottom = []
@staticmethod
def is_win(slots: list) -> bool:
if len(set(slots)) == 1:
return True
return False
@staticmethod
def is_big_win(slots: list) -> bool:
if ['?', '?', '?'] == slots:
return True
return False
def is_lose(self, slots: list) -> bool:
if self.is_win(slots) or self.is_big_win(slots):
return False
return True
def try_luck(self) -> tuple:
self.top = [choice(self.items) for i in range(3)]
self.middle = [choice(self.items) for i in range(3)]
self.bottom = [choice(self.items) for i in range(3)]
timg = "...." + "".join(self.top) + "....\n" + \
"↦ " + "".join(self.middle) + " ↤" + "\n" + \
"...." + "".join(self.bottom) + "....\n"
return (timg,
self.is_big_win(self.middle),
self.is_win(self.middle),
self.is_lose(self.middle)
)
Ответы (1 шт):
Автор решения: Эникейщик
→ Ссылка
Убрать все ненужные пустые строки.
Выкинуть все if-ы
@staticmethod
def is_win(slots: list) -> bool:
return len(set(slots)) == 1
@staticmethod
def is_big_win(slots: list) -> bool:
return ['?', '?', '?'] == slots
def is_lose(self, slots: list) -> bool:
return not (self.is_win(slots) or self.is_big_win(slots))