Python程序测试水的状态给了celcius不正确的答案

时间:2018-06-15 19:47:26

标签: python python-3.x

该程序应该采取两个输入,度数单位为F或C,温度然后打印出水的状态。无论我为C做什么,除了1000我得到答案它是液体,从不冷冻或气体。这是我的代码:

print("Do you use Fahrenheit (F) or Celcius? (C)")
degreeUnit = (input("Enter F or C:")).upper()
temp = int(input("What is the temperature of the water?"))
if degreeUnit == "F" and temp <=32: #checking if unit is F and if water is frozen
    print("The water is frozen.")
elif degreeUnit == "F" and temp >= 212: #water freezes at 32F is gas at 212 is it gas?
    print("The water is a gas")
elif degreeUnit == "F" and temp >=33 or temp <=211:
    print("The water is a liquid.")
elif degreeUnit == "C" and temp <=0: #water freezes at 0C
    print("The water is frozen.")
elif degreeUnit == "C" and temp >=100: #water is gas at 100C
    print("The water is a gas.")
elif degreeUnit == "C" and temp >=1 or temp <=99:
    print("The water is a liquid.")
else:
    print("Please try again, something went wrong.")
input()

我觉得我错过了一些明显的东西,但我一遍又一遍地读它,看不出我做错了什么。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:2)

如果你总是得到液体,你应该检查液体状况。这真的是你想要的吗? (提示,它不是)。检查最后一个elif语句的逻辑,你对F&#39; F&#39; (自由单位:-D)

您可能还会发现包括parens以查看您的逻辑是有帮助的:

a=False
b=True
c=True
a and b or c
>>>True
a and (b or c)
>>>False

答案 1 :(得分:0)

使用括号对条件进行分组。

使用此:

print("Do you use Fahrenheit (F) or Celcius? (C)")
degreeUnit = (input("Enter F or C:")).upper()
temp = int(input("What is the temperature of the water?"))
if degreeUnit == "F" and temp <=32: #checking if unit is F and if water is frozen
    print("The water is frozen.")
elif degreeUnit == "F" and temp >= 212: #water freezes at 32F is gas at 212 is it gas?
    print("The water is a gas")
elif degreeUnit == "F" and (temp >=33 or temp <=211):
    print("The water is a liquid.")
elif degreeUnit == "C" and temp <=0: #water freezes at 0C
    print("The water is frozen.")
elif degreeUnit == "C" and temp >=100: #water is gas at 100C
    print("The water is a gas.")
elif degreeUnit == "C" and (temp >=1 or temp <=99):
    print("The water is a liquid.")
else:
    print("Please try again, something went wrong.")
input()

<强>输出

Do you use Fahrenheit (F) or Celcius? (C)
Enter F or C:'C'
What is the temperature of the water?0
The water is frozen