Issue with my code

def pytha_chal(A):
ans = []
a = 0
while a * a <= A//2:
b = 0
while b * b < A:
if a * a + b * b == A:
print(’(’,str(int(a)),’,’,str(int(b)),’)’,end=’ ')
# newEntry = [a, b]
# ans.append(tuple(newEntry))
b += 1
a += 1
return ans

n=int(input())
num=[]
for i in range(n):
num.append(int(input()))

for k in num:
pytha_chal(k)
print()

Hi @ritvikagrawal1
Request you to share the code through the Coding Blocks IDE
As you can see, the indentation of the code you pasted here is all messed up and it has become very difficult to debug.

def pytha_chal(A):
	ans = []
	a = 0
	while a * a <= A//2:
		b = 0
		while b * b < A:
			if a * a + b * b == A:
				print('(',str(int(a)),',',str(int(b)),')',end=' ')
				# newEntry = [a, b]
				# ans.append(tuple(newEntry))
			b += 1
		a += 1
	return ans


n=int(input())
num=[]
for i in range(n):
	num.append(int(input()))

for k in num:
	pytha_chal(k)
	print()

Here is the corrected code:

def pytha_chal(A):
	a = 0
	while a * a <= A//2:
		b = 0
		while b * b <= A:
			if a * a + b * b == A:
				print('(', str(int(a)), ',', str(int(b)), ')', sep='', end=' ')
			b += 1
		a += 1


n=int(input())
num=[]
for i in range(n):
	num.append(int(input()))

for k in num:
	pytha_chal(k)
	print()

The line causing the error:

while b * b < A:

Correct Line:

while b * b <= A:

In the wrong line, you’ll miss cases of the form (0, b^2)
e.g. For 9, You’ll never check the case (0, 3)

You can mark the doubt as resolved now in the My Doubts section.
Happy Learning!

WOrked thanks!
Also start inner loop with b=a rather than b=0

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.