19 Python作业

身高预测:
男性**身高= (父亲身高+母亲身高) *0.54(cm)
女性**身高= (父亲身高*0.923+母亲身高) /2(cm)
如果喜爱体育锻炼,那么身高可增加2%;
如果有良好的饮食习惯,可增加身高1.5%。

要求:输入性别、父母身高、是否爱好体育锻炼、是否有良好的饮食习惯,输出预测结果。

这个萌新想不太出来。


请先 登录 后评论

最佳答案 2021-09-23 23:10


权供参考:
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()
请先 登录 后评论

其它 6 个回答

瑶小兔M
##关于计算身高的百分比计算可能有误,请自行检查
**=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,"厘米")
请先 登录 后评论
可小可大
不太严谨啊   觉得 input 性别的时候就要加校验
请先 登录 后评论
檬娜
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")
请先 登录 后评论
嫣大大


dad = int(input('父亲的身高'))
mum = int(input('母亲的身高'))
xb = int(input('男性输入1,女性输入0'))
sport = int(input('是否喜欢锻炼,喜欢输入1,否则输入0'))
food = int(input('是否有良好的饮食习惯,是,输入1,否,输入0'))

if xb == 1:
    l0 = (dad+mum)*0.54
    if sport == 1:
        l0 = l0*1.02
    else:
        l0 = l0
    if food == 1:
        l0 = l0*1.015
    else:
        l0 = l0
else:
    l0 = (dad*0.923+mum)/2
    if sport == 1:
        l0 = l0*1.02
    else:
        l0 = l0
    if food == 1:
        l0 = l0*1.15
    else:
        l0 = l0
print(l0)

请先 登录 后评论
舵手
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('再见!')
请先 登录 后评论
Bertha02
  1. def inputValue() -> 'all valid info':
  2.     while True:
  3.         try:
  4.             g = input('\nyour gender?\nenter \'m\' for male enter \'f\' for female: '.title())
  5.             if g.lower() not in ['m', 'f']:
  6.                 raise ValueError
  7.             break
  8.         except ValueError:
  9.             print('\ninvalid value!: '.title(), g, end='\n')
  10.             continue
  11.     while True:
  12.         try:
  13.             f = int(input('enter father height: '.title()))
  14.             break
  15.         except ValueError:
  16.             print('\ninvalid value!: '.title(), f, end='\n')
  17.             continue
  18.     while True:
  19.         try:
  20.             m = int(input('enter mother height: '.title()))
  21.             break
  22.         except ValueError:
  23.             print('\ninvalid value!: '.title(), m, end='\n')
  24.             continue
  25.     while True:
  26.         try:
  27.             s = input('\nlove to exercise?\nenter \'y\' for yes enter \'n\' for no: '.title())
  28.             if s.lower() not in ['y', 'n']:
  29.                 raise ValueError
  30.             if s.lower() == 'y': s = True
  31.             else: s = False
  32.             break
  33.         except ValueError:
  34.             print('\ninvalid value!: '.title(), s, end='\n')
  35.             continue
  36.     while True:
  37.         try:
  38.             e = input('\ngood eating habits?\nenter \'y\' for yes enter \'n\' for no: '.title())
  39.             if e.lower() not in ['y', 'n']:
  40.                 raise ValueError
  41.             if e.lower() == 'y': e = True
  42.             else: e = False
  43.             break
  44.         except ValueError:
  45.             print('\ninvalid value!: '.title(), e, end='\n')
  46.             continue
  47.     return [g, f, m, s, e]

  48. def calculateValue(x: '[g, f, m, s, e]') -> 'calculate':
  49.     if x[0] == 'm': height = (x[1]+x[2])*0.54
  50.     else: height = ((x[1]*0.923)+x[2])/2
  51.     if x[3] == True: height += height*0.02
  52.     if x[4] == True: height += height*0.015
  53.     return height

  54. def displayAnswer(h: 'height') -> 'show the answer':
  55.     print('\nresult: '.title()+str(h)+'CM')

  56. def main():
  57.     displayAnswer(calculateValue(inputValue()))

  58. if __name__ == '__main__':
  59.     main()
复制代码
请先 登录 后评论
  • 7 关注
  • 0 收藏,425 浏览
  • 千四儿 提出于 2021-09-23 23:10

相似问题