简体   繁体   English

Python:如何组合多个if else语句

[英]Python : How to combine multiple if else statements

hi i am newbie and programming and i got stuck in this exercise.嗨,我是新手和编程,我被困在这个练习中。 this is a code to check if water is liquid, solid, or gas, at a given temperature in sea level.这是一个代码,用于检查在海平面的给定温度下水是液体、固体还是气体。 the user is asked to enter if they want to enter Celsius or Fahrenheit temperature.要求用户输入是要输入摄氏温度还是华氏温度。

when user type F and enter a temperature it shows the proper text but it also shows the "Error.Enter valid unit."当用户键入 F 并输入温度时,它会显示正确的文本,但也会显示“Error.Enter valid unit”。 at the end.在末尾。 But when user type C, it does not happen.但是当用户键入 C 时,它不会发生。 It seems like after executing user input "F" block, it also executes user input "C" afterwards so that it goes to the else block.似乎在执行用户输入“F”块之后,它还执行用户输入“C”,以便它转到 else 块。

How do I make it so that it only executes the user input "F" block only and then exit如何使它只执行用户输入的“F”块然后退出

I know it might sound vague im sorry im new to programming我知道这听起来可能很模糊,对不起,我是编程新手

here is my code:这是我的代码:

temp_unit = input("Enter a temp unit (c or f): ")

#user input F
if temp_unit == "F" or temp_unit == "f":
    temp = float(input("Enter a temp in F: "))

    if temp <= 62.6 and temp > 28.4 and temp < 212:
        print("Water is liquid.")

    elif temp <= 28.4:
        print("Water is solid.")

    elif temp >= 212:
        print("Water is gaseous.")

#user input C
if temp_unit == "C" or temp_unit == "c":
    temp = float(input("Enter a temp in C: "))

    if temp <= 17 and temp > -2 and temp < 100:
        print("Water is liquid.")

    elif temp <= -2:
        print("Water is solid.")

    elif temp >= 100:
        print("Water is gaseous.")

#error
else:
    print("Error. Enter valid unit.")

Here is user input "F" executed code这是用户输入“F”执行的代码

Here is user input "C" executed code这是用户输入“C”执行的代码

The second if should be elseif to relate to the first if statement第二个if应该是elseif以与第一个if语句相关

Code fix:代码修复:

temp_unit = input("Enter a temp unit (c or f): ").lower()

if temp_unit == "f":
    temp = float(input("Enter a temp in F: "))
    if temp <= 28.4:
        print("Water is solid.")
    elif 28.4 < temp <= 212:
        print("Water is liquid.")
    else:
        print("Water is gaseous.")
elif temp_unit == "c":
    temp = float(input("Enter a temp in C: "))
    if temp <= 0
        print("Water is solid.")
    elif 0 < temp <= 100:
        print("Water is liquid.")
    else:
        print("Water is gaseous.")
else:
    print("Error. Enter valid unit.")

It's because of the second 'if' statement.这是因为第二个“if”语句。 By using 'if', you force python to run into the second condition.通过使用'if',您可以强制python 进入第二个条件。 You should use 'elif' instead.你应该改用'elif'。 In this case, when the first 'if' statement is true python will not go into the elif statement.在这种情况下,当第一个 'if' 语句为真时,python 不会进入 elif 语句。

See the code below:请参阅下面的代码:

temp_unit = input("Enter a temp unit (c or f): ")

if temp_unit == "F" or temp_unit == "f":
    temp = float(input("Enter a temp in F: "))

    if temp <= 62.6 and temp > 28.4 and temp < 212:
        print("Water is liquid.")

    elif temp <= 28.4:
        print("Water is solid.")

    elif temp >= 212:
        print("Water is gaseous.")


elif temp_unit == "C" or temp_unit == "c":
    temp = float(input("Enter a temp in C: "))

    if temp <= 17 and temp > -2 and temp < 100:
        print("Water is liquid.")

    elif temp <= -2:
        print("Water is solid.")

    elif temp >= 100:
        print("Water is gaseous.")

else:
    print("Error. Enter valid unit.")

You only need two temperatures for this.为此,您只需要两个温度。 The one between solid and liquid, and the one between liquid and gaseous.一种介于固体和液体之间,一种介于液体和气体之间。

temps = {
    "F": [212, 28.4],
    "C": [100, 0],
}

temp_unit = input("Enter a temp unit (c or f): ").strip().upper()

if temp_unit in temps:
    temp = float(input(f"Enter a temp in {temp_unit}: "))
    if temp > temps[temp_unit][0]:
        print("Water is gaseous.")
    elif temp > temps[temp_unit][1]:
        print("Water is liquid.")
    else:
        print("Water is solid.")
else:
    print("Enter valid temp unit.")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM