Processing math: 100%

CPSC150A
Scientific Computing

Activity 21

While Loops

Rewrite For

Rewrite the following function that uses a for loop to use a while loop instead.

import test

def even_total(maximum: int) -> int:
    total: int
    i: int
    total = 0
    for i in range(2, maximum, 2):
        total = total + i
    return total

def main() -> None:
    test.equal(even_total(2), 0)
    test.equal(even_total(10), 20)
    test.equal(even_total(100), 2450)
    test.equal(even_total(101), 2550)
    return None

main()

Validate Input

Create a Python program that prompts the user to enter a single digit number. If 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:
0

Babylonian Square Root

One use of loops is to perform approximations of slightly complicated mathematical operations. One such operation is 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 in use 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.

The Babylonian square root method uses and accumulator to repeatedly compute a better approximation. The initial approximation is n2. A better approximation of the square root, s, is computed with the equation:

s=12×(s+ns)

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()
  • Writing this function can be broken into two steps, computing the approximation, and stopping the the loop. To simplify things, solve each of these steps one at a time by computing the approximation in a for loop that runs a fixed number of times. Once this is working convert the for loop, to a while loop.
  • The condition for the while loop should compare the error of the approximation to 0.00001. Note that depending on whether the approximation is too high or too low the error may be positive or negative. Be sure to compensate for this either when computing the error or comparing 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. A better initial value of s can be computed using the formula:

s=4×10(d1)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. In fact, with the optimal strategy the correct number can be found 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 the user guesses 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 0 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!
  • 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 for a guess before the loop, or initialze 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.

Example

I'm thinking a of a number between 0 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.