Here is a correction of your code.
n=int(input())
counter = 1
for counter in range(1,n):
a = counter*counter
if a >= n:
break
print(a)
There were three things wrong with your code. First, the condition you want to break on is a >= n
not a < n
. Second, that condition needs to be tested before you print the number. Thus the if
statement needs to be inside the for
loop and before your print
, statement. Third, the outer while
loop is not really necessary :) Though you can add it, but a simple inner for
loop will suffice.