0

When I try to make the programm asking the user to select the current unit of the temperature, the if unit == "f" or "F" run, even though unit != "f" or "F".

unit = str(input("Is your temperature currently in Fahrenheit(F) or Celsius(C)? Please enter F or C"))
if unit == "f" or "F":
  print("Your entered temperature will now be transformed from Fahrenheit to Celsius")
  new_tempC = (temp - 32) * 5/9
  print(f"{temp} F expressed in Celsis is {new_tempC} C.")
elif unit == "c" or "C":  
  print("Your entered temperature will now be transformed from Celsius to Fahrenheit")
  new_tempF = (temp * 9/5) - 32
  print(f"{temp} C expressed in Fahrenheit is {new_tempF} F.")
else:
  print("Please enter a valid input.")´´´´

1 Answers1

2

Try:

if "F":
    print('Always true!')

if unit == "f" or "F":    # The condition "F" is always true, hence this will be executed regardless of the value stored in unit.

Should be:

if unit == "f" or unit == "F":

elif unit == "c" or "C":

Should be:

elif unit == "c" or unit == "C":  
Dipen Dadhaniya
  • 4,550
  • 2
  • 16
  • 24