Python-正确返回石头、纸、剪刀的值



我正在尝试编写一个Python脚本,用户可以选择"r"、"p"或"s",脚本将根据返回的值生成Rock,Paper,Scissors游戏的结果。这就是我目前所拥有的:

from random import choice
import sys
# Input name
meetname = input("Nice to meet you! What's your name? n")
print(' n')
# Game instructions
rpsthink = 'Well, ' + meetname + ", how about we play a game of Rock, Paper, Scissors?"
print(rpsthink)
print('n')
#Ask user for choice of rock, paper, scissors
try:
user_move = str(input("Enter 'r' for rock, 'p' for paper, or 's' for scissors. The system will randomly select a choice, and the result of the game will be displayed to you. You can enter 'Q' to quit the game at any time. n"))
except ValueError:
print("Please make sure your input is 'r', 'p', 's', or 'Q'!")
sys.exit()
print('n')
if user_move =='Q':
print('Sorry to see you go - hope you had fun playing!')
sys.exit()
# Generate a random computer choice
def computer_rps() -> str:
#Computer will randomly select from rock, paper, scissors:
computermove: str = choice(['r','p','s'])
return computermove
def gameresult(user_move, computermove):
# Return value based on comparison of user and computer moves
# User wins
if (user_move == 'r' and computermove == 's') or (user_move == 'p' and computermove == 'r') or (user_move == 's' and computermove =='p'):
return 1
# User loses
if (user_move == 'r' and computermove == 'p') or (user_move == 's' and computermove == 'r') or (user_move == 'p' and computermove == 's'):
return -1
# Tie game
if user_move == computermove:
return 0
#Notification of game result based on returned function value
if int(gameresult(user_move, computermove)) == -1:
print("The computer made a choice of ", computermove)
print("Looks like the computer won this time...don't let that deter you - let's have another round for a shot at victory!")
if int(gameresult(user_move, computermove)) == 1:
print("The computer made a choice of ", computermove)
print('Looks like you won! Excellent choice! But how many times can you make the winning decision...?')
if int(gameresult(user_move, computermove)) == 0:
print("The computer made a choice of ", computermove)
print("Looks like it's a tie game - how about another round to settle the score?")
sys.exit()

但是,我收到一个错误,没有为if int(gameresult(user_move, computermove)) == -1行定义名称"computermove"。我是否需要将computermove设置为全局变量,以便可以正确地比较用户和计算机的移动?

computermove是未定义的,因为它在您访问它的范围内不可用,而且您在其他任何地方都没有创建它。您需要创建一个变量,该变量接收生成计算机移动的函数返回的值。

computermove = computer_rps() # Add this line here and it should do it
#Notification of game result based on returned function value
if int(gameresult(user_move, computermove)) == -1:
print("The computer made a choice of ", computermove)
print("Looks like the computer won this time...don't let that deter you - let's have another round for a shot at victory!")
if int(gameresult(user_move, computermove)) == 1:
print("The computer made a choice of ", computermove)
print('Looks like you won! Excellent choice! But how many times can you make the winning decision...?')
if int(gameresult(user_move, computermove)) == 0:
print("The computer made a choice of ", computermove)
print("Looks like it's a tie game - how about another round to settle the score?")
sys.exit()

这个错误很明显。它告诉你,变量computermove没有在你试图使用它的行上定义

在调用gameresult函数之前,需要将所述变量定义为函数computer_rps的返回值。

例如:

computermove = computer_rps()
if int(gameresult(user_move, computermove...

最新更新