Python practice questions Calculating the area of a circle

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 maththe 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 rthe value of the radius

r=float(input("Press radius:"))

Next, use powthe function to calculate the area, ssave it in a variable, and output
powhow 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:
test 1
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()

test2


Guess you like

Origin blog.csdn.net/weixin_45122104/article/details/125976202