0
votes

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." at the end. But when user type C, it does not happen. It seems like after executing user input "F" block, it also executes user input "C" afterwards so that it goes to the else block.

How do I make it so that it only executes the user input "F" block only and then exit

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

Here is user input "C" executed code

3
temp <= 62.6 and temp > 28.4 and temp < 212 wha's the point of checking 3 temperature points? - Tomalak

3 Answers

1
votes

The second if should be elseif to relate to the first if statement

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.")
0
votes

It's because of the second 'if' statement. By using 'if', you force python to run into the second condition. You should use 'elif' instead. In this case, when the first 'if' statement is true python will not go into the elif statement.

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.")
0
votes

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.")