I wrote the program about Pythagorean I got my answer but python doesn’t read (break) after (if) .
My program title : (Special Pythagorean triplet),
there is a one answer exist for ((a+b+c=1000)&(a**2 + b**2 =c**2) )
I want find abc .
I know (a=200, b=375, c=425)
but when program starts it never stop and it continue.
It also types the product of these three numbers.
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
Advertisement
Answer
There’s no need for the pyth
variable. You can just use return True
or return False
.
The if
statement needs to be indented so it’s in the loop.
You need to test the value of the function call.
You don’t need else: continue
. Loops automatically continue unless you break out of them. continue
is only needed when you want to skip the rest of the loop body and start the next iteration; it’s not needed at the end of the body.
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