これは、MIT OCWのコンピュータサイエンスの第7回講義で遭遇したコードです。この小さなプログラムは基底と高さの入力を受けて、ピタゴラスの定理で斜辺を計算します。Python 3が浮動小数点型()を認識しています
何らかの理由により、floatのエントリを認識できませんでした。次のように
コードは次のとおりです。
#! /Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5
import math
#Get base
inputOK = False
while not inputOK:
base = input("Enter base: ")
if type(base) == type(1.0):
inputOK = True
else:
print("Error. Base must be a floating point number.")
#Get Height
inputOK = False
while not inputOK:
height = input("Enter height: ")
if type(height) == type(1.0):
inputOK = True
else:
print("Error. height must be a floating point number.")
hyp = math.sqrt(base*base + height*height)
print("Base: " + str(base) + ", height: " + str(height) + ", hypotenuse:" + str(hyp))