How can a loop that just keeps squaring guesses decide whether a number is a perfect square — and know exactly when to give up?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
You are checking whether x is a perfect square, and the search is currently sitting on guess. Compute the square of the current guess and assign that number to square.
Hint: ** raises a number to a power.
The guess-then-test procedure
State the steps: pick an initial guess, test it against the problem, move to the next candidate on failure, and report no solution once the finite set of guesses runs out.
Perfect-square search with a while loop
Write while guess**2 < x: guess += 1 with the if/else after it separating guess**2 == x from guess**2 > x, and trace both x = 4 and x = 10.
The neg_flag for negative input
Record that the loop body never runs for x = -2, then write neg_flag = False, the if x < 0 update, and the closing check that offers the positive value.