pytorch python方法比较

bq8i3lrv  于 2022-11-09  发布在  Python
关注(0)|答案(2)|浏览(144)

Rock,Paper,Scissors变量bot具有默认值变量Alex,具有传递到www.example.com的值main.py当我调用方法比较时,我得到一个错误方法比较从secrets导入选择从变体导入变体

玩家.py

class Player:
    name = '',
    choice = ''

    def __init__(self, choise = 'ROCK', name = 'bot'):
        self.name = name
        self.choice = choice

    def whoWins(self, bot, alex):
        if bot.choice > alex.choice:
            print('bot, winner')
        if bot.choice < alex.choice:
            print('Alex, winner')
        if bot.choice == alex.choice:
            print('draw')

主文件名.py

from variants import Variants
from player import Player

bot = Player()
alex = Player(Variants.ROCK, "Alex")
print(bot.whoWins(bot, alex))

变体.py

from enum import Enum

class Variants(Enum):
    ROCK = 1,
    PAPER = 2,
    SCISSORS = 3
waxmsbnn

waxmsbnn1#

Variants的问题在于ROCKPAPER后面的逗号--逗号将值转换为tuple,因此

  • ROCK.value == (1, )
  • PAPER.value == (2, )
  • SCISSORS.value == 3
5tmbdcev

5tmbdcev2#

答:我不得不修改比较的方法,我是这样做的:main.po也保持不变:

> from variants import Variants from player import Player
> 
> bot = Player() alex = Player(Variants.ROCK, "Alex")
> print(bot.whoWins(bot, alex))

variants.py 保持不变

from enum import Enum

class Variants(Enum):
    ROCK = 1
    PAPER = 2
    SCISSORS = 3

玩家.py

from secrets import choice
from variants import Variants

class Player:
    name = '',
    choice = ''

    def __init__(self, choice = Variants.ROCK,  name = 'bot'):
        self.name = name
        self.choice = choice

    def whoWins(self, bot, alex):
        if (bot.choice == Variants.ROCK and alex.choice == Variants.PAPER):
            print('Alex, win!')
        if (bot.choice == Variants.PAPER and alex.choice == Variants.ROCK):
            print('Alex, win!')
        if (bot.choice == Variants.SCISSORS and alex.choice == Variants.SCISSORS):
            print('draw')
        if (bot.choice == Variants.ROCK and alex.choice == Variants.ROCK):
            print('draw!')
        if (bot.choice == Variants.ROCK and alex.choice == Variants.SCISSORS):
            print('Bot, win')
        if (bot.choice == Variants.SCISSORS and alex.choice == Variants.PAPER):
            print('Bot, win')
        if (bot.choice == Variants.SCISSORS and alex.choice == Variants.ROCK):
            print('Alex, win!')
        if (bot.choice == Variants.PAPER and alex.choice == Variants.SCISSORS):
            print('Alex, win!')
        elif (bot.choice == Variants.SCISSORS and alex.choice == Variants.SCISSORS):
            print('draw!')

如果机器人选择〉亚历克斯选择:TypeError:“method”和“method”的示例之间不支持“〉”-错误已消失

"一切都好"

PS C:\Users\user\2> python.exe main.py
Alex, win!
PS PS C:\Users\user\2>

相关问题