python定义函数习题 python定义函数方法

python中定义一个函数,题目如下

class rectangle:

为祁门等地区用户提供了全套网页设计制作服务,及祁门网站建设行业解决方案。主营业务为成都网站建设、成都网站设计、祁门网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

def __init__(self,*args):

if len(args)==2:

self.width,self.height=args[0],args[1]

elif len(args)==1:

self.width,self.height=args[0],args[0]

elif len(args)==0:

self.width,self.height=1,1

else:

raise ValueError("Invalid arguments count")

def perimeter(self):

return 2*self.width+2*self.height

def area(self):

return self.width*self.height

a=rectangle(2,4)

print("area is %.1f"%a.area())

Python编程题:编写函数,计算某班级学生考试的平均分?

def avgScore(scores,n=10):

s = 0

for i in range(len(scores)):

s += scores[i]

return s/n

scores=[90,88,76,45,77,95,66,88,91]

print("按班级人数计算的平均值:{:.2f}".format(avgScore(scores)))

print("按考试人数计算的平均值:{:.2f}".format(avgScore(scores,len (scores))))

求一道Python题,是关于定义函数和身体指数的,谢谢各位大神啦!!!

按照题目要求编写的Python程序如下

def calBMI(height,weight):

BMI=weight/(height*height)

if BMI18.5:

return [BMI,"过轻"]

elif BMI24:

return [BMI,"正常"]

elif BMI28:

return [BMI,"过重"]

else:

return [BMI,"肥胖"]

import re

s=input("请输入你的身高(米)和体重(公斤)【逗号隔开】:")

s1=re.split(r'[,,]',s)

height=float(s1[0])

weight=float(s1[1])

name="李子健"

bmi=calBMI(height,weight)

print("{}的测算结果为:".format(name))

print("BMI:%.2f"%bmi[0])

print(bmi[1])

源代码(注意源代码的缩进)


本文名称:python定义函数习题 python定义函数方法
分享路径:http://myzitong.com/article/dogpghp.html