嘿,编码员!在本教程中,我们将了解 Python 编程的常见问题之一:你能使用 Python 编程语言诊断发烧吗?
发烧是指体温高于正常水平。正常温度因人而异,但通常约为 98.6 °F (37 °C)。发烧不是一种疾病。这通常表明您的身体正在努力抵抗疾病或感染。
用 Python 实现发烧检测
我们将首先询问用户是否要输入摄氏温度或华氏温度。这可以对决策产生重大影响。现在我们将检查输入是否是 C 或 F 或者是否有错误的输入。
1
2
3
4
5
6
7
|
temp = input ( "Would you like to enter your temperature in Celcius or Fahrenheit: " ) if temp.upper() = = "C" : pass elif temp.upper() = = "F" : pass else : pass |
让我们逐个块地获取最终代码。第一个块是当输入的温标为“C”时。在这种情况下,用户可以输入温度,如果温度大于或等于 37.8,则表明该人发烧。否则,这个人就不会发烧。温度转换为浮点温度以便更好地诊断。看下面的代码。
1
2
3
4
5
6
7
8
9
10
11
12
|
temp = input ( "Would you like to enter your temperature in Celcius or Fahrenheit: " ) if temp.upper() = = "C" : result = input ( "Enter your body temprature in Celcuis: " ) r = float (result) if r > = 37.8 : print ( "You've a fever" ) else : print ( "You don't have a fever" ) elif temp.upper() = = "F" : pass else : pass |
我们的下一个块是当输入为“F”时。在本例中,阈值温度为 98.6。其余部分与上面相同。获取输入并将输入转换为浮点数以便更好地分析。看下面的代码片段。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 号
|
temp = input ( "Would you like to enter your temperature in Celcius or Fahrenheit: " ) if temp.upper() = = "C" : result = input ( "Enter your body temprature in Celcuis: " ) r = float (result) if r > = 37.8 : print ( "You've a fever" ) else : print ( "You don't have a fever" ) elif temp.upper() = = "F" : result1 = input ( "Enter your body temprature in Fahrenheit:" ) r1 = float (result1) if r1 > = 98.6 : print ( "You've a fever" ) else : print ( "You don't have a fever" ) else : pass |
我们的最后一个块是当用户给出错误的输入时。在这种情况下,将打印一条简单的语句作为输出。看下面的代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 号
|
temp = input ( "Would you like to enter your temperature in Celcius or Fahrenheit: " ) if temp.upper() = = "C" : result = input ( "Enter your body temprature in Celcuis: " ) r = float (result) if r > = 37.8 : print ( "You've a fever" ) else : print ( "You don't have a fever" ) elif temp.upper() = = "F" : result1 = input ( "Enter your body temprature in Fahrenheit:" ) r1 = float (result1) if r1 > = 98.6 : print ( "You've a fever" ) else : print ( "You don't have a fever" ) else : print ( "Please enter the correct input" ) |
Python 中发烧检测的完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 号
|
temp = input ( "Would you like to enter your temperature in Celcius or Fahrenheit: " ) if temp.upper() = = "C" : result = input ( "Enter your body temprature in Celcuis: " ) r = float (result) if r > = 37.8 : print ( "You've a fever" ) else : print ( "You don't have a fever" ) elif temp.upper() = = "F" : result1 = input ( "Enter your body temprature in Fahrenheit:" ) r1 = float (result1) if r1 > = 98.6 : print ( "You've a fever" ) else : print ( "You don't have a fever" ) else : print ( "Please enter the correct input" ) |
一些示例输出
Would you like to enter your temperature in Celcius or Fahrenheit: C Enter your body temprature in Celcuis: 100 You've a fever Would you like to enter your temperature in Celcius or Fahrenheit: F Enter your body temprature in Fahrenheit:56 You don't have a fever Would you like to enter your temperature in Celcius or Fahrenheit: j Please enter the correct input |
结论
在本教程中,我们学习了如何使用 Python 编程语言诊断发烧。如果您喜欢本教程,我相信您也会喜欢以下内容!
感谢您的阅读!快乐编码!😁