擅长:python、mysql、java
<p>如前所述,您需要将返回值从<code>input()</code>转换为浮点(以处理除法中的小数):</p>
<pre><code>question = float(input("What is %d %s %d: " % (num1,randop,num2)))
</code></pre>
<p>我不建议这样做,因为错误的输入会使游戏崩溃,所以使用带有<code>try/except</code>块的输入验证:</p>
<pre><code>question = input("What is %d %s %d: " % (num1,randop,num2))
try:
question = float(question)
except ValueError:
print('Invalid or incorrect.')
continue # skip the rest of the code and head straight into the next iteration in the for loop
</code></pre>
<p>另外,我不建议包含除法选项,因为大多数值都是循环小数,除非事先检查答案不会循环,或者将答案四舍五入到小数点后2位,并要求用户提供2个d.p答案,否则无法正确输入</p>
<p>(也可以将'answer'变量设置为字符串,而不是将'question'设置为int)</p>