在Python中实现发烧检测的方法

319 阅读3分钟

嗨,编码员!在本教程中,我们将学习一个经常被问及的Python编程问题,你能用Python编程语言诊断发烧吗?

发烧是一种高于正常的体温。正常体温可能因人而异,但通常在98.6 °F (37 °C)左右。发烧不是一种疾病。它通常是你的身体试图对抗疾病或感染的一个信号。

在Python中实现发烧检测

我们将首先询问用户是以摄氏度还是华氏度输入温度。这在决策中会产生很大的差异。现在我们将检查输入的是摄氏还是华氏,或者是否有一个错误的输入。

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,那么这个人就发烧了。否则,这个人就没有发烧。为了更好地诊断,温度被转换为浮点数。请看下面的代码。

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。其余部分仍与上面一样。为了更好地分析,把输入的内容转换成浮点数。看看下面的代码片断。

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

我们的最后一个块是当用户给出错误的输入时。在这种情况下,一个简单的语句被打印出来作为输出。请看下面的代码。

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中发烧检测的完整代码

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编程语言来诊断发烧。