Python Program to Calculate the Radius of a Circle

In this tutorial, you will learn about a Python program to calculate the radius of a circle.

You can calculate the radius of a circle using the same formula A = π r².

So let’s dive into it and find out how you can calculate the radius of a circle in Python.

Simple Code

#Python Program to calculate the radius of a circle
pi = 3.14
while True: 
    try:
        rad = input("Enter radius : ")
        rad = float(rad)
        area = pi * rad**2
        print('The area of the circle with radius',rad , 'is', area)
        break
    except:
        print('Enter a valid radius.')

Learn about the while loop in detail here.

Output

The area of the circle with radius 3.0 is 28.26

Code With Explanation

#Python Program to calculate the radius of a circle
pi = 3.14

#If user enter invalid input, let say some character or anything other than a number, This Program will throw an error
#This loop with run program until a valid input is given
while True: 
    
    #this try block will be executed if the input is a number
    try:
        #taking input from the user
        rad = input("Enter radius : ")
        #convert the input into float to avoid any conflict after multiplication with pi
        rad = float(rad)
        #Calculating the area of the circle using the formula A = π r²
        area = pi * rad**2
        #Printing the output on the screen
        print('The area of the circle with radius',rad , 'is', area)
        #If the program is successful this break command will terminate the main while loop
        break
    #If the input was not a number, program will throw this error and will ask again for the input
    except:
        print('Enter a valid radius.')
Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x