python - 如何在我的代码中使用输入?
问题描述
我从用户那里得到一个输入:
planet = input("Planet: ")
假设用户的输入是“mars”。我想要以下代码:
mars[2] * math.sin(mars[3])
我怎么做?
解决方案
这看起来像您想要一个参考表,可能实现为dict
. 您不要使用输入字符串作为变量名:这在语法上很难做到,而且通常是危险的、不好的做法等。相反,尝试这样的事情:
ref = {
"mars": (4, "red", 135, 1.244),
"earth": (3, "blue", 93, 0.000)
}
planet = input("Planet: ")
x = ref[planet][2] * math.sin(ref[planet][3])
这会让你感动吗?
推荐阅读
- java - How to return a file from a controller inside an entity?
- datagrid - Apache Royale:什么是 SDK 0.9.8#2556 上等效的 Jewel DataGrid rowHeight 属性
- xcuitest - XCUITest testing a label that includes new lines
- javascript - How do I make RTL for ionRangeSlider?
- javascript - 单击 SVG 的特定部分
- python - 我在 spyder 中得到了这个: AttributeError: 'int' object has no attribute 'subs'
- couchdb - 如何禁用 fauxton 接口?
- cucumber - (Cucumber) 多个标签无法正常工作
- ios - Associatedtype 取决于协议
- excel - 在VBA函数中计算二变量函数的值