主文件
from variants import Variants
from player import Player
bot = Player
alex = Player(Variants.SCISSORS, 'Alex')
print(bot.whoWins(bot, alex))
播放器.py
from variants import Variants
class Player:
def __init__(self, choise=Variants.ROCK, name='bot'):
self.choise = choise
self.name = name
def whoWins(self, player1, player2):
if (player1 == player2):
print("draw")
if player1 > player2:
print('player1 wins')
if player1 < player2:
print('player2 wins')
变体.py
from enum import Enum
class Variants(Enum):
ROCK = 1,
PAPER = 2,
SCISSORS = 3
错误:
Ошибка: Traceback (most recent call last):
File "D:\shlak\Zadanie k 2 modulu\main.py", line 8, in <module>
print(bot.whoWins(bot, alex))
TypeError: Player.whoWins() missing 1 required positional argument: 'player2'
代码中有很多问题。
主要的一点是类可以使用等于/大于/小于进行比较,它们必须具有魔术方法
__eq__,__gt__, (理想情况下,您仍然需要为,__lt__创建方法)。<=>=我将添加到
Variants方法__eq__中,__lt__其余部分是使用装饰器创建的total_ordering。在方法中,
whoWins我比较的不是类Player,而是他们的选择(字段choice)。total_ordering请参阅装饰器的文档您可以在 Habré: A Guide to Magic Methods in Python的文章中阅读有关魔术方法的一般信息
结论:
bot wins