0

So I wrote a little script just for practice for converting temperatures. It worked and then I'm not sure what I did but no matter what I do it always only uses the Fahrenheit part of the if statement. Any help would be greatly appreciated.

#!/usr/bin/python3

measurement_choice = str(input('Enter either f for Fahrenheit or c for Celsius: '))
temp = int(input('Enter temperature: ')
if measurement_choice == 'F' or 'f':
    f2c = (temp - 32) * 5.0/9.0
    print(temp, 'F converts to', f2c, 'C')
elif measurement_choice == 'C' or 'c':
    c2f = 9.0/5.0 * temp + 32
    print(temp, 'C converts to', c2f, 'F')
else:
    print('Don\'t be dumb')
  • `if measurement_choice.lower() == 'f'` OR `if measurement_choice in ('F', 'f')` OR `if measurement_choice == 'F' or measurement_choice == 'f'` – Olvin Roght Nov 09 '21 at 17:05
  • `measurement_choice == 'F' or 'f'` will always be `True` because `'f'` is a Truthy value. The check is basically this: `if choice == 'F' or True`. You have to make the comparison on both values like this: `if choice == 'F' or choice == 'f'` – Hampus Larsson Nov 09 '21 at 17:06
  • 3
    Does this answer your question? [Why does "a == x or y or z" always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true) – Hampus Larsson Nov 09 '21 at 17:07
  • 1
    Thank you both for that. I realized I asked a question that was already basically posed in another post. I appreciate you both taking the time to answer this. – AverageatBest Nov 09 '21 at 17:23

0 Answers0