<p>第一:您的<code>operator</code>是一个字符串,而不是函数。你不能调用<code>'/'(2,3)</code>,所以如果<code>operator=='/'</code>,你仍然不能调用<code>operator(2,3)</code>。</p>
<p>第二个:<code>int(rn1), int(rn2)</code>是如何将两个不同的数字转换为整数,而不是<code>int(rn1, rn2)</code>。</p>
<p>第三:来自<code>randint()</code>的返回值已经是整数,不需要再次转换。</p>
<hr/>
<p>我建议在输入数字时将其转换为整数<em>,只转换一次</em>,而不是对每个引用都这样做。因此:</p>
<pre><code>minimum = int(easygui.enterbox(msg="Choose your minimum number"))
maximum = int(easygui.enterbox(msg="Choose your maximum number"))
operator = easygui.enterbox(msg="which operator would you like to use? X,/,+ or - ?", title="operator")
questions = int(easygui.enterbox(msg="enter your desired amount of questions"))
# Select a function associated with the chosen operator
operators = {
'*': lambda a,b: a*b,
'/': lambda a,b: a/b,
'+': lambda a,b: a+b,
'-': lambda a,b: a-b,
}
operator_fn = operators.get(operator)
if operator_fn is None:
raise Exception('Unknown operator %r' % operator)
for a in range(questions):
rn1 = randint(minimum, maximum))
rn2 = randint(minimum, maximum))
answer = int(easygui.enterbox("%s %s %s = ?" % (rn1, operator, rn2)))
realanswer = operator_fn(rn1,rn2)
if answer == realanswer:
print "Correct"
else:
print 'Incorrect, the answer was', realanswer
</code></pre>