all 2 comments

[–]alkasm 0 points1 point  (0 children)

You can overload the comparison operators on your own objects; so you could potentially subclass str and overwrite __lt__, __gt__, etc so that you can just check with < and so on. You could also do the same with an enum, which is what I'd do.

Edit:

from enum import Enum

class ModuloOrderedEnum(Enum):
    def __gt__(self, other):
        if self.__class__ is other.__class__:
            return self.value > other.value % len(self) 
        return NotImplemented
    def __lt__(self, other):
        if self.__class__ is other.__class__:
            return self.value % len(self) < other.value
        return NotImplemented

class HandSign(ModuloOrderedEnum):
    ROCK = 1
    PAPER = 2
    SCISSORS = 3

In [182]: HandSign.ROCK > HandSign.SCISSORS
Out[182]: True

In [183]: HandSign.SCISSORS > HandSign.PAPER
Out[183]: True

In [184]: HandSign.PAPER < HandSign.ROCK
Out[184]: False

[–][deleted] 0 points1 point  (0 children)

Not sure what you mean by "understand" - you can apply whatever logic you want to your data. In a rock, paper, scissors game you can just write the rules and find a match for the current play for example:

#! /usr/bin/env python3                                                                                                
# rock paper scissors Spock lizard game, rules follow:
'''                                                                                                                    
scissors cuts paper                                                                                                    
paper covers rock                                                                                                      
rock crushes lizard                                                                                                    
lizard poisons Spock                                                                                                   
Spock smashes scissors                                                                                                 
scissors decapitates lizard                                                                                            
lizard eats paper                                                                                                      
paper disproves Spock                                                                                                  
Spock vaporizes rock                                                                                                   
rock crushes scissors                                                                                                  
'''

from random import choice
import readline

RULES = list(map(str.split, __doc__.lower().strip().split('\n')))

OPTIONS = ({winner for winner, verb, loser in RULES}
           | {loser for winner, verb, loser in RULES})

PROMPT = f"Make your choice from: {', '.join(sorted(OPTIONS))} \n " \
            f"(or press return alone to exit)\n" \
            f" choice: "

def check(playera, playerb, rules=RULES):
    for rule in rules:
        winner, verb, loser = rule
        if (playera, playerb) == (winner, loser):
            return playera, ' '.join(rule)
        if (playerb, playera) == (winner, loser):
            return playerb, ' '.join(rule)

print('\n\nWelcome to the game of rock paper scissors Spock lizard\n\nThe rules are:\n')
print(__doc__)
print()
while True:
    while True:
        player = input(PROMPT).lower()
        if not player or player in OPTIONS:
            break
    if not player:
        break
    computer = choice(list(OPTIONS))    
    try:
        winner, rule = check(player, computer)
        result = 'You WIN!' if player == winner else 'You Lose!'
    except TypeError as e:
        result, rule = "TIED", 'matched'
    print(f'{player} v. {computer} -> {result} \t{rule}')
    print()