"Scissors" and 'Rock' "-6ren">
gpt4 book ai didi

python - Python 中的石头、布、剪刀(大于和小于)

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:06 31 4
gpt4 key购买 nike

有没有一种方法可以用大于号和小于号来做剪刀石头布游戏?

我知道执行 RPS 有不同的方法,但我想具体了解大于和小于。

这是我的代码:

"Rock" > "Scissors" and 'Rock' < 'Paper'
"Paper" > "Rock" and 'Scissors' < 'Rock'
"Sissors" > "Paper" and 'Paper' < 'Scissors'
choose1 = input("Player One, Enter your answer ")
choose2 = input("Player Two, Enter your answer ")

if choose1 == "Paper":
"Paper" > "Rock"
if choose1 == "Scissor":
"Scissor" > "Rock"


if choose1 != "Rock" and choose1 != "Paper" and choose1 != "Scissors":
print("Player one please chose Rock, Paper, or Scissors")

if choose2 != "Rock" and choose2 != "Paper" and choose2 != "Scissors":
print("Player two please chose Rock, Paper, or Scissors")

if choose1 > choose2:
print('Player1 ({}) beats ({})'.format (choose1, choose2))
else:
print('Player2 ({}) beats ({})'.format (choose2, choose1))

游戏运行正常,但是,它认为石头打败一切,布只打剪刀,剪刀打不败任何东西。

如何修复此代码以使其正确执行?

最佳答案

如果你真的想用<> ,制作类,简单的字符串将不符合您的需求。

class Rock:
def __gt__(self, other):
return isinstance(other, Scissors)

class Paper:
def __gt__(self, other):
return isinstance(other, Rock)

class Scissors:
def __gt__(self, other):
return isinstance(other, Paper)


CHOICES = {
"rock": Rock(),
"paper": Paper(),
"scissors": Scissors()
}

a = CHOICES["rock"]
b = CHOICES["scissors"]

print("player a wins:", a > b)

编辑:或者只用一个类可能更好

class RPS:
table = {
"rock": "scissors",
"paper": "rock",
"scissors": "paper"
}

def __init__(self, what):
self.what = what
self.winsover = self.table[what]

def __eq__(self, other):
return self.what == other.what

def __gt__(self, other):
return self.winsover == other.what


a = RPS("rock")
b = RPS("scissors")

print("player a wins:", a > b)

关于python - Python 中的石头、布、剪刀(大于和小于),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32588094/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com