我感到惊讶的是,每个人都跳出来认为问题在于浮点比较。你们应该先看一看完整的问题/代码,然后才能得出结论并迅速回答。在
我们回到正题上来。我不想解释浮点比较的问题。我不是在看嵌套的while循环。我将考虑到这样一个事实,即当计算结果为整数时,作者需要打破循环。在
猫鼬,
当“c”是整数时,您希望循环中断。但你的情况是“int(c)!=c“并不像你想的那样经常检查。
1这将在进入循环时进行检查。届时,“c”的值将为2.51984209979
2下一个检查只有在内部的所有循环都完成之后才会进行。届时,c值将为25.7028456664
你要做的就是每次重新计算“c”的值。
你的代码可能是这样的from myro import *
from math import *
def main():
z = 3
a = 2
b = 2
x = 3
y = 3
lim = 25
c = (a**x + b**y)**(1.0/z)
#while int(c) != c:
while z <= lim:
while a <= lim:
while b <= lim:
while x <= lim:
while y <= lim:
c = (a**x + b**y)**(1.0/z)
print a, b, c, x, y, z
if int(c) == c:
print str(a) + "^" + str(x) + " + " + str(b) + "^" + str(y) + " = " + str(c) + "^" + str(z)
return
y = y + 1
y = 3
print a, b, c, x, y, z
x = x + 1
x = 3
print a, b, c, x, y, z
b = b + 1
b = 3
print a, b, c, x, y, z
a = a + 1
a = 3
print a, b, c, x, y, z
z = z + 1
print "code cycle complete. no numbers meet criteria"
main()