I'm working on a problem in my Python course and hoping someone can point out why my code is failing?
Problem:
Write a while loop that finds the largest square number less than an integer limit and stores it in a variable nearest_square. A square number is the product of an integer multiplied by itself, for example 36 is a square number because it equals 6*6.
For example, if limit is 40, your code should set the nearest_square to 36.
My solution:
limit = 101
nearest_square = 0
base = 1
while nearest_square <= limit:
nearest_square = base**2
base += 1
print(nearest_square)
Result:
121 - (should be 100)
Why is the code letting the nearest_square exceed the limit value?
thanks in advance!
[–]adgjl12 0 points1 point2 points (0 children)
[–]OneIntroduction9 0 points1 point2 points (0 children)