Exercise: Looping using while 2

3.7.1.2. Exercise: Looping using while 2#

A therapist measures a patient’s maximal shoulder flexion angle several times. Write a program that creates a list of all these measurements based on user input, following this example:

Example of program output

Enter max shoulder flexion (deg), or Enter to stop: <User enters 119>
Enter max shoulder flexion (deg), or Enter to stop: <User enters 124>
Enter max shoulder flexion (deg), or Enter to stop: <User enters 123>
Enter max shoulder flexion (deg), or Enter to stop: <User presses Enter>
Max shoulder flexion: [119.0, 124.0, 123.0]

Tip

The input function returns an empty string "" when the user simply presses Enter without entering a value.

# Initialize an empty list of measurements
measurements = []

# Initialize a boolean variable that controls when we need to quit the loop
continue_asking = True

# Ask the values
while continue_asking:
    str_value = input("Enter max shoulder flexion (deg), or Enter to stop: ")
    if str_value == "":
        continue_asking = False
    else:
        measurements.append(float(str_value))

# Print the result
print("Max shoulder flexion:", measurements)