权供参考: class Height: '''一个预测身高的类''' def __init__(self, name): '''初始化属性''' self.name = name def forecast(self): '''接收输入并开始预测''' while True: gender = input("请输入性别(男/女): ") if ('男' not in gender) and ('女' not in gender): print("输入有误!请重新输入。") continue try: father_height = float(input("\n\t请输入你父亲的身高(单位cm): ")) mother_height = float(input("\n\t请输入你母亲的身高(单位cm): ")) hobby = input("你平时喜欢体育锻炼吗?(输入yes/no)") if ('yes' not in hobby) and ('no' not in hobby): print("输入有误! 请输入:yes/no") continue diet = input("平时有良好的饮食习惯吗?(输入yes/no)") if ('yes' not in diet) and ('no' not in diet): print("输入有误! 请输入:yes/no") continue except ValueError: print("你的输入有误! 请输入数字。") continue else: if gender == '男': if (hobby == 'yes') and (diet == 'yes'): height = (father_height + mother_height) * 0.54 + 0.035 print(f"{self.name} {int(height)}cm") break elif (hobby == 'yes') and (diet == 'no'): height = (father_height + mother_height) * 0.54 + 0.02 print(f"{self.name} {int(height)}cm") break elif (hobby == 'no') and (diet == 'yes'): height = (father_height + mother_height) * 0.54 + 0.015 print(f"{self.name} {int(height)}cm") break else: if gender == '女': if (hobby == 'yes') and (diet == 'yes'): height = (father_height * 0.923 + mother_height ) /2 + 0.035 print(f"{self.name} {int(height)}cm") break elif (hobby == 'yes') and (diet == 'no'): height = (father_height * 0.923 + mother_height) /2 + 0.02 print(f"{self.name} {int(height)}cm") break elif (hobby == 'no') and (diet == 'yes'): height = (father_height * 0.923 + mother_height) /2 + 0.015 print(f"{self.name} {int(height)}cm") break if __name__ == '__main__': my = Height(input("请输入你的名字: ")) my.forecast() |
##关于计算身高的百分比计算可能有误,请自行检查 **=input("请输入性别(填写男或女):") father=input("请输入父亲的身高:") father=float(father) mother=input("请输入母亲的身高:") mother=float(mother) sports=input("你是否喜爱运动(填写喜爱或不喜爱):") habbit=input("你认为自己的饮食习惯是否良好(填写良好或不良好):") if **=="男": if sports=="喜爱": if habbit=="良好": height=father*0.5598+mother*0.5598 else: height=fsther*0.408+mother*0.408 else: if habbit=="良好": height=father*0.5481+mother*0.5481 else: height=father*0.54+mother*0.54 else: if sports=="喜爱": if habbit=="良好": height=father*0.4776525+mother*0.5175 else: height=father*0.47073+mother*0.51 else: if habbit=="良好": height=father*0.4684225+mother*0.0075 else: height=father*0.4615+mother*0.5 print("身高预测:",height,"厘米") |
import re import sys list1 = ["性别:","父亲身高:","母亲身高:","是否爱好体育锻炼:","是否有良好的饮食习惯"] def fun(x): a = input(x, ) return a #判断是否为数字 def is_number(num): pattern = re.compile(r'^[-+]?[-0-9]\d*\.\d*|[-+]?\.?[0-9]\d*$') result = pattern.match(num) if result: return True else: return False def num(y): if is_number(y) is True: num = float(y) return num else: print("请输入数字") sys.exit() father = num(fun(list1[1])) mother = num(fun(list1[2])) def gender(e): if e == "男": b = ( father + mother ) * 0.54 return b elif e == "女": b = ( father * 0.923 + mother) / 2 return b else: print("请输入性别(男或女)") sys.exit() b = gender(fun(list1[0])) def hab(z): if z == "是": return True elif z == "否": return False else: print("请输入是或否") sys.exit() def tiyu(g,b): if g == True: b = b * 1.02 return b elif g == False: b = b return b else: pass b1 = tiyu(hab(fun(list1[3])),b) def ys(h,b1): if h == True: d = b1 * 1.015 return d elif h == False: d = b1 return d else: pass b2 = ys(hab(fun(list1[4])),b1) print("预测身高为:","%.2f"%b2,"cm") |
|
def predict(gender,fatherH,motherH,loveSports,goodEating): ''' 身高预测 男性身高 height = (fatherH + motherH) * 0.54 女性身高 height = (fatherH * 0.923 + motherH)/2 热爱运动 height += height * 0.02 良好饮食 height += height * 0.015 ''' height = 0 if gender == '男': height = (fatherH + motherH) * 0.54 else: height = (fatherH * 0.923 + motherH)/2 if loveSports: height += height * 0.02 if goodEating: height += height * 0.015 return height while True: args = input('请输入性别、父亲身高、母亲身高、是否热爱运动、是否良好饮食,各参数以空格分隔.输入0退出').split(' ') if args[0] == '0': break result = predict(args[0],int(args[1]),int(args[2]),eval(args[3]),eval(args[4])) print(result) print('再见!') |
|