¿Por qué Python no lee la siguiente línea (si)?
Escribí el programa sobre Pythagorean. Obtuve mi respuesta, pero Python no lee (rompe) después de (si). El título de mi programa: (Triplete de Pitágoras especial), hay una respuesta para ((a+b+c=1000)&(a**2 + b**2 =c**2) )I want find a b c. Lo sé, (a=200, b=375, c=425)pero cuando el programa comienza, nunca se detiene y continúa. También escribe el producto de estos tres números.
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
Respuestas
No hay necesidad de la pythvariable. Puede usar return Trueo return False.
La ifdeclaración debe tener sangría para que esté en el bucle.
Necesita probar el valor de la llamada a la función.
No es necesario else: continue. Los bucles continúan automáticamente a menos que se salga de ellos. continuesolo es necesario cuando desea omitir el resto del cuerpo del bucle y comenzar la siguiente iteración; no es necesario al final del cuerpo.
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)
break