我的作業
要怎麼在float 內找出整數我也想了一下,後來想到
四捨五入後的數字絕對不等於原來數字
15.0四捨物入後還是等於15
#為了之後要測試float內的值是否為整數,所以定義了一個回傳小數以下進位成整數
#In order to test whether the value in the float is an integer later, it is defined to return a decimal number to be rounded into an integer
import math
def math1(num):
A = math.ceil(num)
return A
#直角三角形三邊長不超過100,又都是整數的直角三角形畢氏定理
#The length of the three sides of a right triangle does not exceed 100 with integers (Pythagorean theorem).
def hypotenuse(x,y):
if x > 100 or x == 0 : # 100以內或是不為零 Less than 100 or not zero
return False
elif y > 100 or y == 0 : # 100以內或是不為零 Less than 100 or not zero
return False
elif math1(x)!=x or math1(y)!=y:
# x和y一定要輸入整數或是後面是.0的數字,如果輸入15.1之類的非整數會先被剛剛定義的math1淘汰
#x and y must be entered as integers or numbers followed by .0. If you enter a non-integer like 15.1, it will be eliminated by the just-defined math1
return False
else:
z = math.sqrt(x**2 + y**2) # z**2 = x**2 + y **2
math1(z)
# 得到的z一定要整數或是後面是.0的數字,如果得到15.1之類的非整數會先被剛剛定義的math1淘汰
#The Z obtained must be an integer or a number followed by .0. If a non-integer such as 15.1 is obtained, it will be eliminated by the just-defined math1
if z != math1(z):
return False
elif z>100: # z不超過100 #z does not exceed 100
return False
else:
return z # z的邊長 #side z
#TEST
print(hypotenuse(5.0,12.0))
print(hypotenuse(5,12))
print(hypotenuse(0,0))
print(hypotenuse(1,0))
print(hypotenuse(7,24))
print(hypotenuse(1,1))
print(hypotenuse(-1,1))
print(hypotenuse(111,1))
print(hypotenuse(1,555))
print(hypotenuse(3,7.8))
print(hypotenuse(2.55,7.8))
print(hypotenuse(1,1))
print(hypotenuse(2.55,9))
結果