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:
-
Valid Input:
- Input:
5
- Expected Output:
The area of the circle with radius 5.0 is: 78.5
- Input:
-
Zero Input:
- Input:
0
- Expected Output:
The area of the circle with radius 0.0 is: 0.0
- Input:
-
Negative Input:
- Input:
-3
- Expected Output:
The radius cannot be negative.
- Input:
-
Invalid Input:
- Input:
abc
- Expected Output:
Invalid input! Please enter a numeric value for the radius.
- Input:
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.