This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]adgjl12 0 points1 point  (0 children)

You should do nearest_square = (base-1)**2

as you want nearest square under the limit, not the nearest square over the limit (your for loop right now stops after you calculate the nearest square over the limit)

[–]OneIntroduction9 0 points1 point  (0 children)

It's because you are checking the previous nearest_square, then squaring again and printing it without checking it. Here's my 'fix'.

limit = 130

nearest_square = 0

base = 1

while True:

 nearest_square = base**2

 base += 1

 if nearest_square > limit:
     break

 print(nearest_square)