topic description
Enter the radius of the circle, calculate the area of the circle and print it out,并保留三位小数
Input sample:
5
Sample output:
78.540
Code
First, import math
the module for calculating the square of the radius
import math
Assign the value of pi π
to a variable for easy use
pi=math.pi
Get r
the value of the radius
r=float(input("Press radius:"))
Next, use pow
the function to calculate the area, s
save it in a variable, and output
pow
how to use the function:pow()
s=pow(a,b) #求a的b次方
Calculate the area and output:
s=pi*(pow(r,2))
print("Area is:{:.3f}".format(s))
The complete code is as follows:
import math
pi=math.pi
r=float(input("Press radius:"))
s=pi*(pow(r,2))
print("Area is:{:.3f}".format(s))
Running results:
BUT, if the article ends like this, you will definitely say that my article is too watery and it must be too simple.
So, our goal today is to write a program for calculating the area with functions
First, write a function for calculating the area
def circle():
r=float(input("Press radius:"))
s=pi*(pow(r,2))
print("Area is:{:.3f}".format(s)) #print('Area is:%.*f' %(3,s))
The reason why the parameters are not written is that this function has no parameters
Then, call the function
circle()
The complete code is as follows:
import math
pi=math.pi
def circle():
r=float(input("Press radius:"))
s=pi*(pow(r,2))
print("Area is:{:.3f}".format(s))
circle()