python - 从字典中调用特定值

标签 python dictionary

我已经为分支文本冒险设置了一个,但我想知道如何使用函数组织和调用特定值。

这是我所拥有的:

choices = {
1: {'prompt': {
    'prompt': 'Do you want a dog?',
    'A': 'Yes',
    'B': 'No',
    'C': 'Maybe'},
    }
}


def print_situation(prompt):
    print(prompt)
    print(A)
    print(B)
    print(C)

理想情况下,我希望能够调用 print_situation(1) 并让它打印出以下内容:

Do you want a dog?
Yes
No
Maybe

知道了这一点,调用print_situation 函数中的值的正确方法是什么?考虑到我的字典设置方式,我有点坚持执行此操作的正确方法,所以任何帮助都会很棒。

最佳答案

def print_situation(prompt):
  print(choices[prompt]['prompt']['prompt'])
  print(choices[prompt]['prompt']['A'])
  print(choices[prompt]['prompt']['B'])
  print(choices[prompt]['prompt']['C'])

关于python - 从字典中调用特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40614311/

相关文章:

python - 无需互联网访问即可在 python 中使用 Docker 组合

swift - 键 : String value:Array in swift 的扩展字典

Python - 如果元素尚未包含在列表中,如何追加到列表中?

python - 在python中实现移位寄存器的最简单方法

python - python 中的协程在 3.4 和 3.5 之间,如何保持向后兼容性?

python - 将函数内的多值字典返回为漂亮的格式

c++主要外部函数之间的共享列表

python - 更改Python中多维字典的值,其中嵌套字典包含在列表中

python - 数据库查询中的NameError

python - 在 Google-Colab 中使用 Python3 创建词云时应该如何使用 Arial 字体?