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
Aucun commentaire:
Enregistrer un commentaire