1、反向输出
def print(n):
print(“n”[::-1])
2、函数编写
def m(n):
if n==1:return 0.5
return n/(n+1)+factorial(n-1)
for n in range(1,n):
print(n,'m(n)='factorial(n))
3、计算三角形面积
import math
class Trangle:
#定义三个点的类属性
def Point1(self, x1, y1):
self.x1 = x1
self.y1 = y1
def Point2(self, x2, y2):
self.x2 = x2
self.y2 = y2
def Point3(self, x3, y3):
self.x3 = x3
self.y3 = y3
#求三条边的距离
def Distance1(self):
self.a = pow(pow(int(self.x1) - int(self.x2), 2) + pow(int(self.y1) - int(self.y2), 2), 0.5)
print("第一条边长为", self.a)
def Distance2(self):
self.b = pow(pow(int(self.x1) - int(self.x3), 2) + pow(int(self.y1) - int(self.y3), 2), 0.5)
print(“第二条边长为:”, self.b)
def Distance3(self):
self.c = pow(pow(int(self.x2) - int(self.x3), 2) + pow(int(self.y2) - int(self.y3), 2), 0.5)
print(“第三条边长为:”, self.c)
#通过海伦公式用三条边求面积
def Area(self):
s = (self.a + self.b + self.c) / 2
area = pow((s * (s - self.a) * (s - self.b) * (s - self.c)), 0.5)
print(“三角形的面积为:”, area)
a = Trangle()
a.Point1(input(‘请输入x1:’), input(‘请输入y1:’))
a.Point2(input(‘请输入x2:’), input(‘请输入y2:’))
a.Point3(input(‘请输入x3:’), input(‘请输入y3:’))
a.Area()
4、输入一个毫秒进行转化
def ms_to_hours(millis):
millis = int(millis)
seconds = (millis / 1000) % 60
seconds = int(seconds)
minutes = (millis / (1000 * 60)) % 60
minutes = int(minutes)
hours = (millis / (1000 * 60 * 60)) % 24
return("%d:%d:%d" % (hours, minutes, seconds))
5、海龟图,两两相连
import turtle
def con_line(xpoint_set,ypoint_set):
“”“将输入的多个点两两相连”""
turtle.pensize(2)#调整画笔粗细
turtle.speed(1)
n=0
for i,j in zip(xpoint_set,ypoint_set):
turtle.penup()
turtle.goto(i,j)
turtle.pendown()
n += 1
m = 0
for x, y in zip(xpoint_set, ypoint_set):
m += 1
if m >= n:
turtle.goto(x,y)
turtle.penup()
turtle.goto(i,j)
turtle.pendown()
turtle.hideturtle()#隐藏海龟
turtle.done()