python基礎Day_5函式作業
阿新 • • 發佈:2021-01-27
技術標籤:python
(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)