重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)
你好,根據(jù)圓的面積公式和周長(zhǎng)公式,導(dǎo)入math庫(kù)獲得Π的值(math.pi),利用input函數(shù)獲得圓的半徑值,編寫計(jì)算公式,利用format()函數(shù)指定字符串格式輸出含兩位小數(shù)的圓的面積和周長(zhǎng)。代碼如下:
成都創(chuàng)新互聯(lián)公司專注于寶雞企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),成都商城網(wǎng)站開發(fā)。寶雞網(wǎng)站建設(shè)公司,為寶雞等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
python求圓的面積和周長(zhǎng)
新建并打開一個(gè)空白的python文件(比如:test.py)。使用def關(guān)鍵字定義一個(gè)findArea(r)方法,用來計(jì)算圓的面積。插入語(yǔ)句:“print("圓的面積為%.6f" % findArea(5))”,打印相關(guān)數(shù)據(jù)結(jié)果。在編輯區(qū)域點(diǎn)擊鼠標(biāo)右鍵,在彈出菜單中選擇“運(yùn)行”選項(xiàng)即可。
工具/原料:
聯(lián)想小新Pro14
Win10
Python3.6.5
PyCharm2020.3.5
1、首先在PyCharm軟件中,打開一個(gè)Python項(xiàng)目。
2、在Python項(xiàng)目中,新建并打開一個(gè)空白的python文件(比如:test.py)。
3、使用def關(guān)鍵字定義一個(gè)findArea(r)方法,用來計(jì)算圓的面積。
4、插入語(yǔ)句:“print("圓的面積為%.6f" % findArea(5))”,打印相關(guān)數(shù)據(jù)結(jié)果。
5、在編輯區(qū)域點(diǎn)擊鼠標(biāo)右鍵,在彈出菜單中選擇“運(yùn)行”選項(xiàng)。
6、程序運(yùn)行完畢后,可以看到已經(jīng)成功地計(jì)算圓的面積。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from math import pi
import logging
class Geometrie(object):
"""docstring for Geometrie"""
def __init__(self):
? pass
def say(self):
? print self.__class__.__name__
def compute_area(self):
? pass
def compute_circumference(self):
? pass
def say_cirumfrerence(self):
? print "%s 's cirumfrerence is: %f" % (self.__class__.__name__, self.compute_circumference())
def say_area(self):
? print "%s 's cirumfrerence is: %f" % (self.__class__.__name__, self.compute_area())
class Ellipse(Geometrie):
"""docstring for Ellipse"""
def __init__(self,major_axis, minor_axis):
? """
? major_axis is a
? minor_axis is b
? """
? super(Ellipse, self).__init__()
? if not (isNum(major_axis) and isNum(minor_axis)):
? ? ? raise Exception("TypeError: Please make sure the major:\
? ? ? ?{0!r} and minor {1!r} axis are correct.".format(major_axis, minor_axis))
? else:
? ? ? self.a=major_axis
? ? ? self.b=minor_axis
def compute_circumference(self):
? q=self.a+self.b
? h=(abs((self.a-self.b)/(self.a-self.b)))**2
? m=22/(7*pi)-1
? n=(abs((self.a-self.b)/self.a))**(33.397)
? return pi*q*(1+3*h/(10+(4-3*h)**(0.5)))*(1+m*n)
def compute_area(self):
? return self.a*self.b*pi
class Square(Geometrie):
"""
docstring for Square"Geometrie
"""
def __init__(self, length, width):
? super(Square,self).__init__()
? if not (isNum(length) and isNum(width)):
? ? ? raise Exception("TypeError: Please make sure the length:\
? ? ? ?{0!r} and width {1!r} axis are correct.".format(length, width))
? else:
? ? ? self.a = length
? ? ? self.b = width
def compute_circumference(self):
? return 2*(self.a+self.b)
def compute_area(self):
? return self.a*self.b
class Circle(Geometrie):
"""docstring for Circle"""
def __init__(self, radius):
? super(Circle, self).__init__()
? if not (isNum(radius)):
? ? ? raise Exception("TypeError: Please make sure the radius:\
? ? ? ?{0!r} is correct.".format(radius))
? else:
? ? ? self.r = radius
def compute_circumference(self):
? return (2*self.r)*pi
def compute_area(self):
? return pi*(self.r**2)
def isNum(value):
try:
? value + 1
except TypeError:
? return False
else:
? return True
def main():
"""
docstring for main
"""
Es = Ellipse(2,1)
Es.say_cirumfrerence()
Es.say_area()
Sq = Square(2,1)
Sq.say_cirumfrerence()
Sq.say_area()
Cr = Circle(4)
Cr.say_cirumfrerence()
Cr.say_area()
if __name__ == '__main__':
main()
python輸入半徑求圓的面積的具體代碼如下:
#輸入圓半徑,求圓周長(zhǎng)和圓面積
r=eval(input()) #以實(shí)現(xiàn)獲取輸入半徑的值
PI=3.1415926
L=2*PI*r #以實(shí)現(xiàn)計(jì)算圓周長(zhǎng)
S=PI*r**2 #以實(shí)現(xiàn)計(jì)算面積
print("圓周長(zhǎng)為","{:.2f}".format(L),",面積為","{:.2f}".format(S),sep="") #以實(shí)現(xiàn)輸出:保留兩位小數(shù)的圓周長(zhǎng)和圓面積
eval() 函數(shù)用來執(zhí)行一個(gè)字符串表達(dá)式,并返回表達(dá)式的值。
語(yǔ)法:eval(expression[, globals[, locals]])
expression --?表達(dá)式。
globals --?變量作用域,全局命名空間,如果被提供,則必須是一個(gè)字典對(duì)象。
locals --?變量作用域,局部命名空間,如果被提供,可以是任何映射對(duì)象。
在日常生活中,我們經(jīng)常會(huì)需要去計(jì)算周長(zhǎng)或者面積.雖然說難度不大,但是很多時(shí)候在寫程序的時(shí)候,比如一張圖片的面積,或者頁(yè)面布局的時(shí)候也是會(huì)需要用到的.
#定義計(jì)算矩形周長(zhǎng)的函數(shù)
def? girth(width,height):
return (width+height)*2
#定義計(jì)算矩形面積的函數(shù)
def area(width,height):
return width*height
if __name__ =='__main__':
print(area(10,20))
print(girth(25,50))
62.83
706.86
import?math?#調(diào)用math函數(shù)
r?=?float(input("輸入圓的半徑:"))
S?=?math.pi*float(r)**2
C?=?2*math.pi*float(r)
print("半徑為{0}的圓的面積為:{1}".format(r,?round(S,2)))
print("半徑為{0}的圓的周長(zhǎng)為:{1}".format(r,?round(C,2)))
希望可以幫到你