python methods in comparison

Viewed 17

Rock, Paper, Scissors variable bot has default value Variable Alex, has values that are passed to main.py When I call method comparison I get an error Method comparisons from secrets import choice from variants import Variants

Player.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')

main.py

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
2 Answers

The problem with Variants is the trailing comma after ROCK and PAPER -- the comma turns the value into a tuple, so

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

Answer: I had to modify the comparison of methods, I did it like this: also main.po remained the same:

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

variants.py remained the same

from enum import Enum

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

Player.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!')

if bot.choice > alex.choice: TypeError: '>' not supported between instances of 'method' and 'method' - The error is gone

All, is ok!

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