# example code, Lecture 7, Fall 2008
import math
#get base
inputOK = False
while not inputOK:
base = float(input('Enter base:'))
#we get str in python 3x from input, we need to convert str to float.
if type(base) == type(1.0): inputOK = True
else: print('Enter, Base must be a floating point number.')
#get height
inputOK = False
while not inputOK:
height = float(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) + '.hyp:' + str(hyp))
import math
def getFloat(requestMsg, errorMsg):
inputOK = False
while not inputOK:
Val = input(requestMsg)
if type(Val) == type(1.0): inputOK = True
else: print(errorMsg)
return Val
base = getFloat('Enter base:', 'Error: base must be a float')
height = getFloat('Enter height:', 'Error: height must be a float')
hyp = math.sqrt(base*base + height*height)
print ('Base:' + str(base) + '.height:' + str(height) + '.hyp:' + str(hyp))
# get base
inputOK = False
while not inputOK:
try:
# user can pass 'inf', 'nan', no error will be raised
# should we check this cases?
base = float(input('Enter base:'))
except ValueError:
print('Base must be an integer or floating point number.')
else:
inputOK = True
Lecture 7_2: Lists and mutability, dictionaries, pseudocode, introduction to efficiency
最新推荐文章于 2022-04-03 15:59:55 发布