<p>你可能要找的问题是:</p>
<pre><code>while hp1 > 0 or hp2 > 0:
#determines original raw acc
char1rawacc = ws1 - ac2
</code></pre>
<p>这个循环永远不会结束,因为无论你在里面做什么都不会改变它的状态。可能你想要一个<code>if</code>。在</p>
<p>剩下的:天哪。看到这个代码很痛苦。让我们做得更好一点。在</p>
<p>不要使用<code>global</code>,除非你有一个非常好的理由这样做。现在,把这看作是一个纪律问题;当你作为一个程序员进步时,你会明白为什么分离作用域很重要。在</p>
<p>使用函数来描述类似的事情一次。关键在于:去掉重复的部分,用名字代替,用新的“单词”让你的语言更接近你正在解决的问题。在</p>
^{pr2}$
<p>使用数据结构。在这种情况下,将一个角色的统计信息合并到一个实体中。在</p>
<pre><code># Use a dict to describe character stats
def get_character_description(name):
description = {}
description['name'] = name
print "Describe character %s" % name
description['strength'] = int(input("%s's strength?"))
# input other values here...
return description
char1 = get_character_description('Pikachu')
if char1['strength'] > 100: ...
</code></pre>
<p>了解类时,请考虑创建一个自定义类来描述字符:</p>
<pre><code>class Character(object):
def __init__(self, name):
self.name = name
def input(self):
print "Let's define stats of %s" % self.name
self.strength = int(input("Strength?"))
# and so on
char1 = Character('J.C. Denton')
if char1.strength > 100: ...
</code></pre>
<p>之后,您的代码可能如下所示:</p>
<pre><code>char1 = get_character_description('Liu Kang')
char2 = get_character_description('Sub Zero')
if char1.wins_over(char2):
print char1.name, "has won"
else:
print char2.name, "has won"
</code></pre>