我编写了关于Pythagorean的程序,我得到了答案,但是python没有在(if)之后阅读(中断)。我的程序标题:(特殊的毕达哥拉斯三重奏),有一个答案存在于((a+b+c=1000)&(a**2 + b**2 =c**2) ),我想找到a_b_c。我知道(a=200, b=375, c=425),但是当程序启动时,它永远不会停止,它还在继续。它还键入这三个数字的乘积。
import random as r
def pyth(b,d,c):
pyth = None
if b**2+c**2 == d**2 :
pyth = True
h=d*c*b
print(h)
return pyth
if b**2+d**2==c**2 :
pyth= True
h=d*c*b
print(h)
return pyth
if d**2 + c**2 == b**2:
pyth =True
h=d*c*b
print(h)
return pyth
else:
pyth = False
return
a = list(range (150,1000))
b=0
c=0
d=0
h = 0
for i in range(0,10000000):
b = r.choice(a)
c = r.choice(a)
d = 1000-b-c
e = b+c+d
if e == 1000 :
pyth(b,d,c)
if pyth == True:
break
else:
continue发布于 2021-01-18 06:15:14
不需要pyth变量。您可以只使用return True或return False。
if语句需要缩进,因此它在循环中。
您需要测试函数调用的值。
你不需要else: continue。循环自动继续,除非您断开它们。只有当您想跳过循环主体的其余部分并开始下一次迭代时,才需要continue;在主体的末尾不需要它。
import random as r
def pyth(b,d,c):
if b**2+c**2 == d**2 :
h=d*c*b
print(h)
return True
if b**2+d**2==c**2 :
h=d*c*b
print(h)
return True
if d**2 + c**2 == b**2:
h=d*c*b
print(h)
return True
else:
return False
a = list(range (150,1000))
for i in range(0,10000000):
b = r.choice(a)
c = r.choice(a)
d = 1000-b-c
e = b+c+d
if e == 1000 and pyth(b,d,c)
breakhttps://stackoverflow.com/questions/65769534
复制相似问题