import math
class Circle():
def __init__(self,r):
self.r=r
def Cir(self):
print("圆的周长为{:.3f}".format(2*math.pi*self.r))
def Area(self):
print("圆的面积为{:.3f}".format(math.pi*self.r**2))
R=float(input("请输入你要求的圆的半径:"))
C=Circle(R)
C.Cir()
C.Area()
"""
海伦公式计算面积
p = (a+b+c)/2
s = math.sqrt(p*(p-a)*(p-b)*(p-c))
"""
import math
class Strange():
def __init__(self,a,b,c):
self.a=a
self.b=b
self.c=c
def get_Cir(self):
print("三角形的周长为{:.3f}".format(self.a+self.b+self.c))
def get_area(self):
if self.a+self.b>self.c and self.a+self.c>self.b and self.b+self.c>self.a and
self.a-self.b
参考案例:(参考第二个成绩打印的方法也可,f是格式化输出)
class Student():
def __init__(self,name,chinese,math,english):
self.name=name
self.chinese=chinese
self.math=math
self.english=english
def get_score(self,score):
if score>=90:
return "A"
elif score>=80:
return "B"
elif score>=70:
return "C"
elif score>=60:
return "D"
else:
return "E"
students=[]
for i in range(3): #为了方便测试,数据少了点
name=input("请输入姓名:")
chinese=int(input("语文:"))
math=int(input("数学:"))
english=int(input("英语:"))
s=Student(name,chinese,math,english)
students.append(s)
for s in students:
print("姓名:",s.name)
print("语文:",s.chinese, "等级:", s.get_score(s.chinese))
print("数学:", s.math, "等级:", s.get_score(s.math))
print("英语:", s.english, "等级:", s.get_score(s.english))
class Student:
def __init__(self,score):
self.score = score
def get_perscore(self):
return self.score
def get_avescore(self):
return scores1/len(scores )
scores = []
for i in range(5):
ss=int(input("请输入第{}个学生成绩:".format(i+1)))
s = Student(ss)
scores.append(s)
scores1=0
for s in scores:
scores1+=s.get_perscore()
print("总分:",scores1 )
print("平均分:", s.get_avescore())
class Gameperson:
def __init__(self, name, blood, majic, status):
self.name = name
self.blood= blood
self.majic= majic
self.status = status
def skill(self):
if self.majic>=20:
self.majic-=5
print("{}释放了技能,剩余魔法点为{}".format(self.name,self.majic))
else:
print("{}释放失败".format(self.name))
def get_damage(self,damage):
self.blood-=damage
print("{}受到了{}伤害,剩余{}血量".format(self.name,damage,self.blood))
if self.blood<=0:
self.status="死亡"
print("您当前状态为{},不好意思,七请您重新开始游戏".format(self.status))
person=Gameperson("小红",30,30,"正常")
print(person)
person.skill()
person.get_damage(10)