石头剪刀斯波克蜥蜴在 Python 中不起作用



我是Python的初学者,我试图了解我做错了什么。我在主功能上遇到困难。除了最后一部分,我在 main 函数中正确完成了大部分代码。你能建议出什么问题吗? ""我正在尝试在玩家和计算机之间创建一个游戏,看看谁赢了。选择是 石头、纸、剪刀、斯波克和蜥蜴。获胜者是通过通过 模数为 5,如果为真或假,则创建一些比较。希望这有助于"">

def choice_to_number(choice):
"""Converts choice into number.
Args:
choice (str): Given game choice.
Returns:
int: Associated number for the given choice.
"""
if choice == "Rock":        # This code converts a choice into a number.
number = 0
elif choice == "Spock":
number = 1
elif choice == "Paper":
number = 2
elif choice == "Lizard":
number = 3
elif choice == "Scissors":
number = 4
else:
print("Invalid choice")
return number
def number_to_choice(number):
"""Converts number into choice.
Args:
number (int): Given game number.
Returns:
str: Associated choice for the given number.
"""
if number == 0:         # This code converts a number into a choice
choice = "Rock"
elif number == 1:
choice = "Spock"
elif number == 2:
choice = "Paper"
elif number == 3:
choice = "Lizard"
elif number == 4:
choice = "Scissors"
else: 
print("Invalid number")
return choice
def rpsls(player_number, comp_number):
"""Plays Rock-Paper-Scissors-Lizard-Spock game between player and computer.
Args:
player_number (int): Player's choice converted into number.
comp_number (int): Computer's choice converted into number.
Returns:
bool: True if player wins the round; False if computer wins the round;
None otherwise.
"""
difference = (player_number - comp_number) % 5      
if difference == 1 or difference == 2:
result = True 
elif difference == 3 or difference == 4:
result = False
else:
result = None
return result 
def main(argv):
"""Main function of the script.
Args:
argv (list): Contains command-line arguments passed to the script.
Returns:
int: Error code after execution (0 if OK).
"""
error_code = 0
# Get player's choice from command line.
player_choice = input('Choose between "Rock", "Paper", "Scissors", "Lizard", or "Spock": >>> ')
# Normalize player's input.
player_choice = player_choice.lower().replace('"', '')
# If player's choice is not valid, display error message and set error code.
if player_choice not in ['rock', 'paper', 'scissors', 'lizard', 'spock']:
print('ERROR: Invalid choice!')
print('Valid choices are "Rock", "Paper", "Scissors", "Lizard", or "Spock"')
print('Try again...')
error_code = 1
# If error code has not been set, play the game.
if error_code == 0:
# Sample a random number between 0 and 4 for computer's choice.
comp_number = random.randrange(0, 5)
# Convert player's choice into number.
player_number = choice_to_number(choice)
# Convert computer's number into choice. (create comp_choice variable somewhere)
comp_number = number_to_choice(number)
# Play Rock-Paper-Scissors-Lizard-Spock 
rpsls(player_number, comp_number)
# Print out the choices of the player and the computer.
print(player_choice)
print(comp_choice)
# Print out the result of the game round.
if difference == 1 or difference == 2:
print("Player wins!") 
elif difference == 3 or difference == 4:
print("Computer wins!")
else: 
print("It's a tie!")
return error_code
if __name__ == '__main__':
error_code = main(sys.argv[1:])
print('[+] Terminated with code: ' + str(error_code))
sys.exit(error_code)

您正在尝试在变量"差异"不存在的地方使用它。相反,使用"rpsls"函数的返回值来查看谁赢了

最新更新