gpt4 book ai didi

python - 石头剪刀布不打印结果

转载 作者:行者123 更新时间:2023-12-01 22:32:29 24 4
gpt4 key购买 nike

我正在尝试针对计算机创建剪刀石头布程序,但遇到了一些问题。

我做了两种方法,一种是计算机的选择,一种是用户的选择。对于计算机来说,它随机生成 0,1,2 并从我声明为局部变量 rps 的数组中选择石头、布或剪刀。当我尝试使用 game_play() 方法运行游戏时,我可以输入输入,但无法在玩家和计算机之间获得结果输出。

class RockPaperScissors():
global rps
rps = ['rock', 'paper','scissors']

def computer_choice(self): #computer randomly picks rock paper or scissors
x = random.randint(0,2)
w = rps[x]
return w
#print(rps[x])

def player_choice(self): #does the player choose rock paper or scissors
x = True
while x:
choice = (input("Player- would you like rock, paper, or scissors? enter rock,paper, or scissors?: "))
if choice.lower() == 'rock':
return 'rock'
elif choice.lower() == 'paper':
return 'paper'
elif choice.lower() == 'scissors':
return 'scissors'
else:
("please enter in rock, paper or scissors")

def game_play(self):
rock = RockPaperScissors()
user = rock.player_choice()
comp= rock.computer_choice()

if comp == 'rock' and user == 'paper':
return "the player wins!"
elif comp == 'rock' and user == 'scissors':
return "the computer wins!"
elif comp == 'paper' and user == 'rock':
return "the computer wins!"
elif comp == 'paper' and user == 'scissors':
return "the player wins!"
elif comp == 'scissors' and user == 'paper':
return"the computer wins!"
elif comp == 'scissors' and user == 'rock':
return "the player wins"

我正在尝试这样测试:

rock = RockPaperScissors()
rock.game_play()

最佳答案

如果您将其作为脚本运行,而不是在 Python 解释器中运行所有内容。您必须显式打印该值。

一种可能的解决方案是:

rock = RockPaperScissors()
print(rock.game_play())

关于python - 石头剪刀布不打印结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62294786/

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