擅长:python、mysql、java
<p>注意:最好的方法是像其他答案所建议的那样使用字典。然而,这里是实际做你所要求的。你知道吗</p>
<p>如果您没有像其他答案所建议的那样将<code>Long_Swordsmenc</code>变量放入字典中,则必须执行以下操作:</p>
<pre><code>import sys
module = sys.modules[__name__]
Long_Swordsmenc = {'as': 4, 'ar': 5, 'de': 2, 'co': 2, 'asr': 7, 'he': 10, 'wm': 1, 'am': 1}
x1 = raw_input('Player 1, enter your unit: ')
unit1 = getattr(module, x1)
print(unit1)
</code></pre>
<p>印刷品:</p>
<pre><code>{'as': 4, 'ar': 5, 'de': 2, 'co': 2, 'asr': 7, 'he': 10, 'wm': 1, 'am': 1}
</code></pre>
<p>编辑:一个更简单的方法是使用<code>globals()[x1]</code>,比如@martineau和@mhlester。更多信息请参见下面的评论。你知道吗</p>