Python 如何使用字典?

Python 如何使用字典?,python,dictionary,Python,Dictionary,我正在尝试做一个字典石头剪刀的游戏,我打算使用很多if和elif,但是我的CS实验室技术人员告诉我我可以使用python中的字典。我怎么用它来做一个石头剪刀的游戏 我的计划是使用一个函数,从列表中抽出一个字母,试着比较游戏中是否有赢的表示“赢”,如果输的表示“输”,如果打成平局,则打印“打成平局”,然后询问你是否想再次玩。但这就是我所能做到的。假设两个玩家,有三种可能的游戏结束状态(赢、输、平)和九种不同的结果(石头、剪刀等)。如果你想把它作为一个字典,你可以创建三个键——每个游戏结束状态一个键

我正在尝试做一个字典石头剪刀的游戏,我打算使用很多if和elif,但是我的CS实验室技术人员告诉我我可以使用python中的字典。我怎么用它来做一个石头剪刀的游戏


我的计划是使用一个函数,从列表中抽出一个字母,试着比较游戏中是否有赢的表示“赢”,如果输的表示“输”,如果打成平局,则打印“打成平局”,然后询问你是否想再次玩。但这就是我所能做到的。

假设两个玩家,有三种可能的游戏结束状态(赢、输、平)和九种不同的结果(石头、剪刀等)。如果你想把它作为一个字典,你可以创建三个键——每个游戏结束状态一个键——其中每个值都是一个列表,其中包含可能导致该结果的游戏。这些游戏可以存储为有序对,其中元组的第一个值代表玩家的选择,第二个值代表对手的选择。e、 例如,所有可能的win状态的键值对如下所示:

"win" : [("rock", "scissors"), ("paper", "rock"), ("scissors", "paper")]
from random import choice

answers = {"yes" : ["yes", "y"],
           "no"  : ["no",  "n"]}

choices = ["rock", "paper", "scissors"]

games = {"win"  : [(choices[0], choices[2]), 
                   (choices[1], choices[0]), 
                   (choices[2], choices[1])],

         "lose" : [(choices[0], choices[1]), 
                   (choices[1], choices[2]), 
                   (choices[2], choices[0])],

         "tie"  : [(choices[0], choices[0]), 
                   (choices[1], choices[1]), 
                   (choices[2], choices[2])]}

print("Let's play \"Rock, paper, scissors\"!\n")

replay = True

while replay:

    player = ""

    while player.lower() not in choices:

        player = input("Rock, paper, or scissors?: ")

    opponent = choice(choices)

    print("You chose {}.".format(player.lower()))
    print("Your opponent chose {}.".format(opponent))

    for outcome in games:

        if (player.lower(), opponent) in games[outcome]:

            print("You {} against your opponent!\n".format(outcome))

    replay_decision = ""

    while replay_decision.lower() not in (answers["yes"] + answers["no"]):

        replay_decision = input("Would you like to play again? [y/n]: ")

        if replay_decision.lower() in answers["no"]:

            replay = False

print("\nThanks for playing!")
一旦你有了所有可能的游戏的
dict
,只需迭代每个结束状态键,并检查玩家和对手所做选择的
元组是否包含在与该键相关的
列表
值中。如果是,那么你已经找到了游戏的结果

考虑到这一点,您可以执行以下操作:

"win" : [("rock", "scissors"), ("paper", "rock"), ("scissors", "paper")]
from random import choice

answers = {"yes" : ["yes", "y"],
           "no"  : ["no",  "n"]}

choices = ["rock", "paper", "scissors"]

games = {"win"  : [(choices[0], choices[2]), 
                   (choices[1], choices[0]), 
                   (choices[2], choices[1])],

         "lose" : [(choices[0], choices[1]), 
                   (choices[1], choices[2]), 
                   (choices[2], choices[0])],

         "tie"  : [(choices[0], choices[0]), 
                   (choices[1], choices[1]), 
                   (choices[2], choices[2])]}

print("Let's play \"Rock, paper, scissors\"!\n")

replay = True

while replay:

    player = ""

    while player.lower() not in choices:

        player = input("Rock, paper, or scissors?: ")

    opponent = choice(choices)

    print("You chose {}.".format(player.lower()))
    print("Your opponent chose {}.".format(opponent))

    for outcome in games:

        if (player.lower(), opponent) in games[outcome]:

            print("You {} against your opponent!\n".format(outcome))

    replay_decision = ""

    while replay_decision.lower() not in (answers["yes"] + answers["no"]):

        replay_decision = input("Would you like to play again? [y/n]: ")

        if replay_decision.lower() in answers["no"]:

            replay = False

print("\nThanks for playing!")
它将生成以下示例输出:

Let's play "Rock, paper, scissors"!

Rock, paper, or scissors?: rock
You chose rock.
Your opponent chose rock.
You tie against your opponent!

Would you like to play again? [y/n]: y
Rock, paper, or scissors?: paper
You chose paper.
Your opponent chose rock.
You win against your opponent!

Would you like to play again? [y/n]: y
Rock, paper, or scissors?: scissors
You chose scissors.
Your opponent chose paper.
You win against your opponent!

Would you like to play again? [y/n]: n

Thanks for playing!

您还可以粘贴以前的代码,并显示您尝试过的内容,以及您期望的和不适合您的内容吗?您可以制作一个dict,如
{'rs':'win','rr':'tie',}
等。这可能对您有用: