擅长:python、mysql、java
<p>问题不是“x没有被重置”。问题是,您的代码并没有被设计为在每次传递中都使用三个运算符。因为,如果1是1的话,那么对于1,这个等式是重复的。在</p>
<p>您需要更像这样的逻辑,即显式地使用三个运算符并迭代三个运算符的所有组合:</p>
<pre><code>from random import randrange
def brute_force():
x1 = randrange(1, 9)
x2 = randrange(1, 9)
x3 = randrange(1, 9)
x4 = randrange(1, 9)
numList = [x1, x2, x3, x4]
operatorList = ["+", "-", "/", "*"]
equation = ""
for i in range(4):
for j in range(4):
for k in range(4):
equation = str(numList[0]) + operatorList[i] + str(numList[1]) + operatorList[j] + str(numList[2]) + operatorList[k] + str(numList[3])
print("equation: " + equation)
print("evaluation: " + str(eval(equation)))
if __name__ == "__main__":
brute_force()
</code></pre>
<p>输出看起来像</p>
^{pr2}$