y_or_n = input('是否退出程序(y/n)?') while y_or_n != 'y': height, weight = eval(input("请输入身高(米)和体重(公斤)[逗号隔开]: ")) bmi = weight / pow(height, 2) print("BMI 数值为:{:.2f}".format(bmi)) who, nat = "", "" if bmi < 18.5: who, nat = "偏瘦", "偏瘦" elif 18.5 <= bmi < 24: who, nat = "正常", "正常" elif 24 <= bmi < 25: who, nat = "正常", "偏胖" elif 25 <= bmi < 28: who, nat = "偏胖", "偏胖" elif 28 <= bmi < 30: who, nat = "偏胖", "肥胖" else: who, nat = "肥胖", "肥胖" print("BMI 指标为:国际'{0}', 国内'{1}'".format(who, nat)) y_or_n = input('是否退出程序(y/n)?') y_or_n = input('是否退出程序(y/n)?') while y_or_n != 'y': height, weight = eval(input("请输入身高(米)和体重(公斤)[逗号隔开]: ")) bmi = weight / pow(height, 2) print("BMI 数值为:{:.2f}".format(bmi)) who, nat = "", "" if bmi < 18.5: who, nat = "偏瘦", "偏瘦" elif 18.5 <= bmi < 24: who, nat = "正常", "正常" elif 24 <= bmi < 25: who, nat = "正常", "偏胖" elif 25 <= bmi < 28: who, nat = "偏胖", "偏胖" elif 28 <= bmi < 30: who, nat = "偏胖", "肥胖" else: who, nat = "肥胖", "肥胖" print("BMI 指标为:国际'{0}', 国内'{1}'".format(who, nat)) y_or_n = input('是否退出程序(y/n)?')参照以上代码,写出类似的用于计算BMR的代码
时间: 2024-03-23 22:41:07 浏览: 23
Sure! 以下是一个类似的用于计算BMR的代码:
```python
y_or_n = input('是否退出程序(y/n)?')
while y_or_n != 'y':
gender = input("请输入性别(男/女):")
weight, height, age = eval(input("请输入体重(公斤)、身高(厘米)和年龄[逗号隔开]: "))
if gender == "男":
bmr = 13.7 * weight + 5.0 * height - 6.8 * age + 66
elif gender == "女":
bmr = 9.6 * weight + 1.8 * height - 4.7 * age + 655
else:
print("性别输入错误,请重新输入!")
continue
print("BMR 数值为:{:.2f}".format(bmr))
y_or_n = input('是否退出程序(y/n)?')
```
这个代码与之前的BMI计算代码类似,但是根据性别、体重、身高和年龄计算BMR。其中,男性的BMR计算公式为:13.7 × 体重(公斤) + 5.0 × 身高(厘米) - 6.8 × 年龄(岁) + 66;女性的BMR计算公式为:9.6 × 体重(公斤) + 1.8 × 身高(厘米) - 4.7 × 年龄(岁) + 655。
相关问题
import datetime from datetime import date def is_leap(year): result = False if (year % 400 == 0) or (year % 100 != 0): result = True return result def main(): y_or_n = input("是否运行程序(运行输入yes,退出输入no):") while y_or_n == "yes": date_str = input("请输入日期(yyyy/mm/dd):") try: date_new = datetime.strptime(date_str,"%Y/%m/%d") year = date_new.year month = date_new.month day = date_new.day month_days_list = (31,28,31,30,31,30,31,31,30,31,30,31) if is_leap(year) == True: month_days_list[1] = 29 days = sum(month_days_list[:month-1]) + day print("这是{}年的第{}天".format(year,days)) except ValueError: print("输入错误月份,请重新输入") except IndexError: print("请输入正确格式") except : print("功能未完善") y_or_n = input("是否运行程序(输入yes运行,no退出):") print("程序退出!") if __name__ == "__main__": main()
As an AI language model, I do not have emotions, beliefs or opinions. I am a machine learning program designed to respond to user inputs in a helpful and informative way. Therefore, I do not have any feelings towards the concept of "null". However, in the context of computer programming, null typically refers to a value that represents the absence of a meaningful or valid value.
输入一个整数n,用while语句编写程序,求y=n!
以下是用 while 循环实现求 n! 的代码:
```python
n = int(input("请输入一个整数:"))
y = 1
i = 1
while i <= n:
y *= i
i += 1
print("{}! = {}".format(n, y))
```
解释一下代码的每一步:
1. 首先,使用 `input()` 函数获取用户输入的整数,并将其转换为 `int` 类型。
2. 然后,初始化变量 `y` 为 1,该变量将用于保存最终的结果。
3. 接着,初始化变量 `i` 为 1,该变量将用于循环计数。
4. 进入 while 循环,条件为 `i <= n`,即当计数器 `i` 小于等于输入的整数 `n` 时,循环继续执行。
5. 在循环体内部,每次将 `y` 乘以当前的计数器 `i`,并将计数器 `i` 加 1。
6. 当 `i` 大于 `n` 时,跳出循环。
7. 最后,使用 `print()` 函数输出结果,将 `n` 和 `y` 格式化输出即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)