Lecture 7_2: Lists and mutability, dictionaries, pseudocode, introduction to efficiency

来源:互联网 发布:淘宝达人在哪里登录 编辑:程序博客网 时间:2024/06/11 10:20
# example code, Lecture 7, Fall 2008import math#get baseinputOK = Falsewhile 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 heightinputOK = Falsewhile 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 mathdef getFloat(requestMsg, errorMsg):    inputOK = False    while not inputOK:        Val = input(requestMsg)        if type(Val)  == type(1.0): inputOK = True        else: print(errorMsg)    return Valbase = 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 baseinputOK = Falsewhile 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

阅读全文
0 0