write and test a program that computes the area of a circle. This program should request a number representing a radius as input from the user. It should use the formula 3.14 * radius ** 2 to compute the area and then output this result suitably labeled.

1 answer

Here's a simple Python program that computes the area of a circle based on the radius provided by the user:

# Function to compute the area of a circle
def calculate_circle_area(radius):
    return 3.14 * radius ** 2

# Main part of the program
if __name__ == "__main__":
    try:
        # Request the radius from the user
        radius = float(input("Please enter the radius of the circle: "))

        # Ensure the radius is non-negative
        if radius < 0:
            print("The radius cannot be negative.")
        else:
            # Compute the area
            area = calculate_circle_area(radius)

            # Output the result
            print(f"The area of the circle with radius {radius} is: {area}")
    except ValueError:
        print("Invalid input! Please enter a numeric value for the radius.")

How to Test the Program:

  1. Valid Input:

    • Input: 5
    • Expected Output: The area of the circle with radius 5.0 is: 78.5
  2. Zero Input:

    • Input: 0
    • Expected Output: The area of the circle with radius 0.0 is: 0.0
  3. Negative Input:

    • Input: -3
    • Expected Output: The radius cannot be negative.
  4. Invalid Input:

    • Input: abc
    • Expected Output: Invalid input! Please enter a numeric value for the radius.

You can run this code in any Python environment (like IDLE, PyCharm, Jupyter Notebook, or any online Python compiler) to see the output according to the various test cases mentioned above.