找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 4505|回复: 6

多多指导

17

主题

85

帖子

85

积分

新手

积分
85
我是吃货 发表于 2018-7-15 22:08:06 | 显示全部楼层 |阅读模式
  1. import sys
  2. print('How old are you?')
  3. age = int(sys.stdin.readline())
  4. if age == 0:
  5.     print('How weight are you?')
  6.     weight = int(sys.stdin.readline())
  7.     if weight >= 3 and weight <= 11:
  8.         print('Your weight is very good!')
  9.     elif weight < 3:
  10.         print('You to thin!')
  11.     else:
  12.         print('You to fat!')
  13. elif age == 1:
  14.     print('How weight are you?')
  15.     weight = int(sys.stdin.readline())
  16.     if weight >= 9 and weight <= 14:
  17.         print('Your weight is very good!')
  18.     elif weight < 9:
  19.         print('You to thin!')
  20.     else:
  21.         print('You to fat!')
  22. elif age == 2:
  23.     print('How weight are you?')
  24.     weight = int(sys.stdin.readline())
  25.     if weight >= 11 and weight <= 15:
  26.         print('Your weight is very good!')
  27.     elif weight < 11:
  28.         print('You to thin!')
  29.     else:
  30.         print('You to fat!')
  31. elif age == 3:
  32.     print('How weight are you?')
  33.     weight = int(sys.stdin.readline())
  34.     if weight >= 13 and weight <= 18:
  35.         print('Your weight is very good!')
  36.     elif weight < 13:
  37.         print('You to thin!')
  38.     else:
  39.         print('You to fat!')
  40. elif age == 4:
  41.     print('How weight are you?')
  42.     weight = int(sys.stdin.readline())
  43.     if weight >= 15 and weight <= 20:
  44.         print('Your weight is very good!')
  45.     elif weight < 15:
  46.         print('You to thin!')
  47.     else:
  48.         print('You to fat!')
  49. elif age == 5:
  50.     print('How weight are you?')
  51.     weight = int(sys.stdin.readline())
  52.     if weight >= 16 and weight <= 21:
  53.         print('Your weight is very good!')
  54.     elif weight < 16:
  55.         print('You to thin!')
  56.     else:
  57.         print('You to fat!')
  58. elif age == 6:
  59.     print('How weight are you?')
  60.     weight = int(sys.stdin.readline())
  61.     if weight >= 17 and weight <= 24:
  62.         print('Your weight is very good!')
  63.     elif weight < 17:
  64.         print('You to thin!')
  65.     else:
  66.         print('You to fat!')
  67. elif age == 7:
  68.     print('How weight are you?')
  69.     weight = int(sys.stdin.readline())
  70.     if weight >= 20 and weight <= 26:
  71.         print('Your weight is very good!')
  72.     elif weight < 20:
  73.         print('You to thin!')
  74.     else:
  75.         print('You to fat!')
  76. elif age == 8:
  77.     print('How weight are you?')
  78.     weight = int(sys.stdin.readline())
  79.     if weight >= 22 and weight <= 30:
  80.         print('Your weight is very good!')
  81.     elif weight < 22:
  82.         print('You to thin!')
  83.     else:
  84.         print('You to fat!')
  85. elif age == 9:
  86.     print('How weight are you?')
  87.     weight = int(sys.stdin.readline())
  88.     if weight >= 24 and weight <= 35:
  89.         print('Your weight is very good!')
  90.     elif weight < 24:
  91.         print('You to thin!')
  92.     else:
  93.         print('You to fat!')
  94. elif age == 10:
  95.     print('How weight are you?')
  96.     weight = int(sys.stdin.readline())
  97.     if weight >= 27 and weight <= 41:
  98.         print('Your weight is very good!')
  99.     elif weight < 27:
  100.         print('You to thin!')
  101.     else:
  102.         print('You to fat!')
  103. else:
  104.     print('How body mass index are you?')
  105.     BMI = int(sys.stdin.readline())
  106.     if BMI >= 18 and BMI <= 25:
  107.         print('Your weight is very good!')
  108.     elif BMI > 25 and BMI <= 30:
  109.         print('You are little heavy!')
  110.     elif BMI > 30 and BMI <= 35:
  111.         print('You are some heavy!')
  112.     elif BMI > 35 and BMI <= 40:
  113.         print('You are very heavy!')
  114.     elif BMI > 40:
  115.         print('You are special emphasis!')
  116.     else:
  117.         print('You are special light!')

复制代码

怎么样?请高手多多指点。

回复

使用道具 举报

3

主题

278

帖子

278

积分

侠客

积分
278

热心会员

uitb 发表于 2018-7-16 14:56:55 | 显示全部楼层
感觉if语句写的太多了,不过看着很清楚明了,python的话,写个嵌套字典或者列表之类的把界定条件写进去,然后根据输入的数值可以很简单的到字典中去查询对应的结果,应该可以很简短的实现相同功能,在代码量上应该至少能缩短一半吧
回复 支持 1 反对 0

使用道具 举报

17

主题

85

帖子

85

积分

新手

积分
85
我是吃货  楼主| 发表于 2018-7-15 22:11:35 | 显示全部楼层
这是检测体重的代码(单位:kg),花了不少时间哦!
回复 支持 反对

使用道具 举报

17

主题

85

帖子

85

积分

新手

积分
85
我是吃货  楼主| 发表于 2018-7-17 12:53:54 | 显示全部楼层
哦………………………………………………………………………………………………………………………………
回复

使用道具 举报

0

主题

5

帖子

5

积分

贫民

积分
5
JeffPython 发表于 2018-10-6 11:46:41 | 显示全部楼层
单个函数不要超过50行。
回复 支持 反对

使用道具 举报

0

主题

2

帖子

2

积分

贫民

积分
2
zhu79 发表于 2018-12-13 14:22:11 | 显示全部楼层
每个if后面的东西基本都是重复的,可以单独写一个,然后用,这样是否会好些?
回复 支持 反对

使用道具 举报

0

主题

16

帖子

16

积分

贫民

积分
16
edo 发表于 2019-11-21 20:23:34 | 显示全部楼层
  1. import sys
  2. input_hint = ["How old are you?", "How weight are you?"]
  3. age = int(input(input_hint[0]))
  4. output_hint = ["You are too thin!", "Your weight is very good!", "You are too fat!"]
  5. W = [[3, 11], [9,14], [], [], [], [], [], [], [], [], []] # 补全这里的代码
  6. if age < 11:
  7.     weight = int(input(input_hint[1]))
  8.     if weight < W[weight][0]:
  9.         print(output_hint[0])
  10.     elif weight <= W[weight][1]:
  11.         print(output_hint[1])
  12.     else:
  13.         print(output_hint[2])

  14. else:
  15.     print('How body mass index are you?')
  16.     BMI = int(sys.stdin.readline())
  17.     if BMI >= 18 and BMI <= 25:
  18.         print('Your weight is very good!')
  19.     elif BMI > 25 and BMI <= 30:
  20.         print('You are little heavy!')
  21.     elif BMI > 30 and BMI <= 35:
  22.         print('You are some heavy!')
  23.     elif BMI > 35 and BMI <= 40:
  24.         print('You are very heavy!')
  25.     elif BMI > 40:
  26.         print('You are special emphasis!')
  27.     else:
  28.         print('You are special light!')
复制代码


修改了前半部分
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表