- Define a function to output an integer in response. For example: input 3245, output 5432. 2. Write a function to calculate the following sequence:
- Sequence calculation
- Enter the coordinates of the three vertices of the triangle. If valid, the area of the triangle will be calculated; if the coordinates are invalid, a prompt will be given
. - Enter a number of milliseconds and convert the number to hours, minutes, and seconds.
- Use turtle drawing. Enter multiple points and connect them in pairs.
(1)
def trace_print(n):
sum = 0
while True:
if n < 1:
break
else:
i = n % 10
sum = sum*10 + i
n //= 10
return sum
m = int(input("请输入一个整数"))
print("这个整数反向输出为{}".format(trace_print(m)))
(2)
def count_m(n):
total = 0
for i in range(1, n+1):
total += (i/(i + 1))
return total
k = int(input("请输入一个整数n: "))
print("这个数列的和为{}".format(count_m(k)))
(3)
import math
def s_tri(x1, x2, x3, y1, y2, y3):
a = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
b = math.sqrt((x1 - x3) ** 2 + (y1 - y3) ** 2)
c = math.sqrt((x2 - x3) ** 2 + (y2 - y3) ** 2)
p = (a + b + c) / 2
if a + b == c or a + c == b or b + c == a:
print("坐标是无效的哦")
s = None
else:
s = math.sqrt(p * (p - a) * (p - b) * (p - c))
return s
x1, y1 = map(int, input("请输入第一个点的坐标").split())
x2, y2 = map(int, input("请输入第一个点的坐标").split())
x3, y3 = map(int, input("请输入第一个点的坐标").split())
print("这三角形的面积为{}".format(s_tri(x1, x2, x3, y1, y2, y3)))
(4)
def convert(ms):
s = ms/1000
minu = s/60
hours = minu/60
print("{:.2f}毫秒是{:.2f}秒,是{:.2f}分," # 保留两位小数输出
"是{:.2f}小时".format(ms, s, minu, hours))
ms = int(input("请输入多少毫秒"))
convert(ms)
(5)
import turtle
def mul_tur(s, n):
t = turtle.Pen()
for i in range(n - 1):
for j in range(i + 1, n):
t.penup()
t.goto(s[i])
t.pendown()
t.goto(s[j])
t.hideturtle()
turtle.done()
s = []
while True:
a = input("输入坐标,按Q/q停止输入:")
if a.upper() == 'Q':
break
rea = eval(a)
s.append(rea)
n = len(s)
mul_tur(s, n)