python-3.x - 使用用户输入作为字典中的值
问题描述
无论如何使用 input() 作为字典中的值?我努力了:
dictionary = {'1': input('Choose: [1]Red [2]Blue'), '2':input('Choose: [1]Green [2]Yellow')}
但这会返回第一个输入:
'Choose: [1]Red [2]Blue'
解决方案
看起来很简单。
>>> dictionary = {'1': input('Choose: [1]Red [2]Blue '), '2':input('Choose:
[1]Green [2]Yellow ')}
Choose: [1]Red [2]Blue 1
Choose: [1]Green [2]Yellow 2
>>> dictionary
{'1': '1', '2': '2'}
>>>
为了代码清晰,您最好将input(...)
值分配给变量,并使用变量构造一个新字典。
推荐阅读
- mongodb - 我想知道如何在做一个 rest api 函数后传递值,而不在 MongDB 中存储数据
- r - 在函数内使用带有 read.csv 的粘贴
- java - 如何从 Edittext 在自定义列表视图中显示值
- c# - NPOI C# - 将一个工作簿的范围单元格复制到另一个工作簿
- php - 我的 apache 模块目录中没有 libphp*.so
- javascript - SCRIPT5007:无法获取未定义或空引用的属性“关闭”
- css - 子级扩展时如何在嵌套的 CSS 网格中扩展父级高度
- django - “如何修复 'Django' 中的 'csrf' 错误?”
- php - 当我单击按钮以在选择选项作为参考的其他表上显示数据时如何修复“MethodNotAllowedHttpException”
- reporting-services - Reporting Services 气泡图未显示所有气泡