Python中的岩石,纸,剪刀游戏

时间:2015-06-09 00:49:15

标签: python

我用Python编写了一个摇滚,纸张,剪刀游戏,当我运行它时一切正常,但它只会在我使用时运行:

def play_game(player1, player2, beats):

    if (player1 == player2):
        return ("Tie")
    elif (player1 == beats[player2]):
        return ("Player 1 wins.")
    elif (player2 == beats[player1]):
        return ("Player 2 wins.")

    beats = {
        'scissors': 'rock',
        'rock': 'paper',
        'paper': 'scissors',
         }


    player1 = input("Please enter either rock, paper, or scissors: ")
        while player1 not in beats.keys():
        player1 = input("That isn't an option. Please try again.")

    player2 = input("Please enter either rock, paper, or scissors: ")
        while player2 not in beats.keys():
        player2 = input("That isn't an option. Please try again.")

    if (player1 == player2):
        print("tie")

    elif(player1 == beats[player2]):
        print("Player 1 wins")

    elif(player2 == beats[player1]):
        print("Player 2 wins")

我有两个部分解释玩家获胜等但是如果我删除了def play_game它仍然可以工作但我删除了底部的'elif'的'if'它不再在def play_game部分下面打印。< / p>

1 个答案:

答案 0 :(得分:0)

因此,如果你删除你从未调用过但没有使用的函数play_game(),没有任何反应......如果你删除文件底部的那些打印谁获胜的行,那么它会停止打印谁获胜?

如果你想使用play_game函数,你必须调用它。通过调用if/elif/elif

替换play_game(..)
def play_game(player1, player2, beats):

    if (player1 == player2):
        return ("Tie")
    elif (player1 == beats[player2]):
        return ("Player 1 wins.")
    elif (player2 == beats[player1]):
        return ("Player 2 wins.")

beats = {
    'scissors': 'rock',
    'rock': 'paper',
    'paper': 'scissors',
 }


player1 = input("Please enter either rock, paper, or scissors: ")
while player1 not in beats.keys():
    player1 = input("That isn't an option. Please try again.")

player2 = input("Please enter either rock, paper, or scissors: ")
while player2 not in beats.keys():
    player2 = input("That isn't an option. Please try again.")

message = play_game(player1, player2, beats)
print message