1

I am doing a program that will convert units from Imperial to Metric, and vice versa. So far, I have only gotten to the temperature conversions. When I run the code, it works fine until it gets to the if statements for "if temputit == ____. No matter what I input, even if it is gibberish, it will still run the Fahrenheit statement (The top one). Here is my code:

unit = input(print("What would you like to convert (Temperature, Distance, Length, Wheight, Volume)? "))
if unit == 'Temperature':
    tempunit = input(print('Do you want to convert Celsius or Fahrenheit?'))
    if tempunit == "Fahrenheit" or "fahrenheit":
        ftemp = int(input(print("Type the number you want to convert: ")))
        ftemp_new = (ftemp - 32) * 5/9
        print(ftemp_new, "°C")
    elif tempunit == "Celsius" or "celsius":
        print("ok")

3 Answers3

4
if tempunit == "Fahrenheit" or "fahrenheit":

This line of code translates to if tempunit == "Fahrenheit" or "fahrenheit" is True. The second part is always true, hence the if block is always executed. See https://realpython.com/python-boolean/ for details.

To fix this, you can do one of the following:

  1. tempunit == "Fahrenheit" or tempunit == "fahrenheit"
  2. tempunit in ["Fahrenheit", "fahrenheit"]
  3. tempunit.lower() == "fahrenheit"
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
4

When you do the line if tempunit == "Fahrenheit" or "fahrenheit": , the second part does not evaluate to if tempunit is equal to 'fahrenheit', it evaluates to fahreneit the string. And a string will always be true so the code will always execute

Try this code to fix it

unit = input(print("What would you like to convert (Temperature, Distance, Length, Wheight, Volume)? "))
if unit == 'Temperature':
    tempunit = input(print('Do you want to convert Celsius or Fahrenheit?'))
    if tempunit == "Fahrenheit" or tempunit == "fahrenheit":
        ftemp = int(input(print("Type the number you want to convert: ")))
        ftemp_new = (ftemp - 32) * 5/9
        print(ftemp_new, "°C")
    elif tempunit == "Celsius" or tempunit == "celsius":
        print("ok")
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Siddharth Agrawal
  • 2,960
  • 3
  • 16
  • 37
  • What does your answer say that mine doesn’t? – Abhijit Sarkar Mar 15 '21 at 04:26
  • 5
    I am sorry. I was writing my answer when you were writing yours, then I left to tinkle and when I completed my answer, I saw you had already posted it. You know it is possible for 2 people to give an answer. That doesnt mean you will downvote my answer. I upvoted your answer because it works just as fine – Siddharth Agrawal Mar 15 '21 at 04:27
  • Thank you so much, this really helped me. – Yaman Siham Mar 15 '21 at 04:42
  • Actually a string is not always True. "" evaluates to False. Try bool("") or if "": print("True") to verify. Regardless, this is the correct answer. – Gregory Morse Mar 28 '21 at 17:12
-1
if "fahrenheit":

is the same as

if bool("fahrenheit"):

is the same as

if "fahrenheit" != "":

So casting a string to a bool is effectively the same as comparison to inequality with an empty string.

if tempunit == "Fahrenheit" or tempunit == "fahrenheit":

is the correct code. Trying to do something like constructing a list and using the in operator or constructing a new string by calling .lower() will be slightly slower do to memory allocation from object construction.

Gregory Morse
  • 369
  • 2
  • 10