Hi all,
I have tried Pythagorean triples program coding …but the output is repeated twice and thrice etc…also (2,0,2) and (4,3,5) should not be included in my triples. please point the location where I have done the mistake.

N=int(input('enter the upper limit: '))

p=int(input('enter a no : '))

while (p<=N):

for n in range(1,p):

if (p<=2) & (p >=(q and r)):
print("INVALID")
else:
if p%2!=0:
q=int(((p*p)/2)-0.5)
r=int(((p*p)/2)+0.5)
#triple=(p,q,r)
print((p,q,r));
elif(p%2==0):
q=int(((p/2)**2)-1)
r=int(((p/2)**2)+1)
#triple=(p,q,r)
print((p,q,r));
else:
print("greater than the natural no N")

I’ve done some reformatting on your code and re-posted here. I trust that I’ve not misinterpreted anything.

N=int(input('enter the upper limit: '))
p=int(input('enter a no : '))
while (p <= N):
for n in range(1,p):
if (p <=2 ) & (p >= (q and r)):
print("INVALID")
else:
if p%2 != 0:
q = int(((p*p)/2)-0.5)
r = int(((p*p)/2)+0.5)
#triple=(p,q,r)
print((p,q,r))
elif(p%2 == 0):
q=int(((p/2)**2)-1)
r=int(((p/2)**2)+1)
#triple=(p,q,r)
print((p,q,r))
else:
print("greater than the natural no N")
p = p+1

The issue with running your code as is, is that both q and r have not been defined before the while loop kicks in and as such the logic is not easy to follow.

Also, I’m unsure what the p = p+1 is for at the end.

edit: hold up, maybe it’s part of the final else statement.
Done.

Thanks a lot...whatever u said the same the same I have worked here. what a coincidence! But still I have one thing that my 'p' should not be greater than 'q' and also 'q' not greater than 'r'. I have changed p<=2 & p>q>r...it still gives output (4,3,5)..Is there any other way to avoid (4,3,5)?

r = 5
q = 4
p = 3
if p <= q:
print('no, p is not > q')
else:
print('yes, p is > q')
if q <= r:
print('no, q is not > r')
else:
print('yes, q is > r')

… not too sure if that’s of any help.

b.t.w: these posts use ‘backtics’ as a way of tagging code (it’s called markdown) so it’s better not to use them of anything else because in doing so, you make it real hard to read your posts.

too many if else makes complicate to reread my pgm…I need short and sweet Rob. btw am able to get ur ‘btw’ .u mean I markeddown my code and sent if am not wrong?

Let me clean up your code to make it written better for Python style.

N = int(input('enter the upper limit: '))
p = int(input('enter a no : '))
while p <= N:
for n in range(1, p):
q = 4
r = 5
if (p <= 2) or (p > q > r):
print(" INVALID NUMBER ")
if p % 2 != 0:
q = int((p*p)/2 - 0.5)
r = int((p*p)/2 + 0.5)
else:
q = int((p/2)**2 - 1)
r = int((p/2)**2 + 1)
assert p**2 + q**2 == r**2
# Pythagorean triple = (p, q, r)
print((p,q,r))
p = p + 1

There is no doubt in Rob’s code compared to my programming way.But am customising the way I like to see my coding part.Thank you Steven, added value to someone helping in the community. More helping hands are here to my knowledge…
But still (2,02),(4,3,5) (please enter 2 instead of 5 in place of “p”)appear in conditions which will not satisfy the pythagorean rules and also what happened to my p>q>r part…Is it really working or not? If you feel I am not clear please let me know in a better manner.