CPSC120
Fundamentals of Computer Science

Activity 19

While Loops

Validate Input

Create a Python program that prompts the user to enter a single-digit number. When the user enters a number that isn't a single-digit number, it should inform the user that the input was invalid and prompt the user for another single-digit number. This should repeat until the user enters a single-digit number. When the user enters a single-digit number, the program should end.

Example

Enter a single digit number:

10

Sorry, 10 is not a single digit number.

Enter a single digit number:

-10

Sorry, -10 is not a single digit number.

Enter a single digit number:

-9

Thanks, -9 is a single digit number.

Babylonian Square Root

One use of loops is to perform approximations of complicated mathematical operations such as square roots. While it may seem impossible to figure out a square root without a calculator, one of the earliest known methods for approximating a square root was used by the Babylonians in the 5th century B.C.E.

Details

Write the Python function babylonian_approximation(n: float) -> float that returns an approximation of the square root of n. The parameter n is a positive float. Because it is not possible to compute a negative number’s square root, the function should assert that n is positive.

The Babylonian square root method uses and accumulator to repeatedly compute a better approximation. The initial approximation is \(\frac{n}{2}\). A better approximation of the square root, s, is computed with the equation:

\[s = \frac{1}{2} \cdot (s + \frac{n}{s})\]

The function should stop improving the approximation when the difference between the approximation squared and n is less than 0.00001.

Test Cases

import test

def babylonian_approximation(n: float) -> float:
    # Put your code here

def main() -> None:
    test.equal(babylonian_approximation(25.0), 5.0)
    # Put more test cases here
    return None

main()

Hint

  • Writing this function can be broken into two steps, computing the approximation and stopping the loop. Simplify the problem by solving each step separately:

    1. Compute the approximation in a for loop that runs a fixed number of times.
    2. Convert the for loop to a while loop.
  • The condition for the while loop should compare the error of the approximation to 0.00001. Depending on whether the approximation is too high or too low, the error may be positive or negative. Compensate for this by comparing the absolute value of the error to 0.00001.

Challenge

You can use any initial value of s to get an approximation for the square root. However, choosing a better starting value will produce a good approximation more quickly. Better initial value of s can be computed using the formula:

\[s = 4 \cdot 10 ^ {\frac{(d - 1)}{2}}\]

Where d is the number of digits in n. Modify the babylonian_approximation function so that it uses this better approximation.

Higher Lower

Higher Lower is a guessing game where one player thinks of a number, and the other player tries to guess it. By letting the guesser know whether their guess was too high or too low, it is possible to hone in on the correct answer pretty quickly. The right number can be found with the optimal strategy in ceil(log2 (n + 1)) guesses, where n is the range of numbers to choose from.

Details

Create a program that plays the game Higher Lower. The program should pick a random number and repeatedly prompt the user to enter a number until they guess the number. After each guess, the program should inform the user if their guess was too high or too low.

Example

I'm thinking a of a number between 1 and 20. Guess the number!

Enter a number:

0

Too low.

Enter a number:

5

Too low.

Enter a number:

10

Too high.

Enter a number:

7

You got it!

Hint

  • Writing while loops can be tricky because often you know when the loop should stop. But the while loop syntax requires a condition of when the loop should continue. One way to simplify this is to negate, use the not operator, the stopping condition.

  • Note that the loop condition depends on the user's guess, so there must be a variable that contains the user's guess before the loop. Either prompt the user to guess before the loop or initialize the variable to something that will never be a correct guess.

Challenge

It's not much of a game if the user can keep guessing until they get it correct. Add a limit to the number of guesses the user has and inform the user how many guesses they have left.

I'm thinking a of a number between 1 and 20. Guess the number!

You have 3 guesses left.

Enter a number:

10

Too low.

You have 2 guesses left.

Enter a number:

15

Too low.

You have 1 guess left.

Enter a number:

17

Too high.

You didn't guess it. It was 16.