I think the best way to explain this will be to post my code, and the results from the shell. I’m new to Python (using 3.3), and coding in general, so I’m sure I’m just missing something very simple.
Here’s my code:
def menu():
global temp
global temp_num
temp = input('Enter a temperature to convert:\n (ex. 32F) -> ')
temp_select = temp[-1]
temp_num = int(temp[:-1])
if temp_select == 'F' or 'f':
return Fahr()
elif temp_select == 'C' or 'c':
return Cels()
elif temp_select == 'K' or 'k':
return Kelv()
else:
print('Please make a valid selection')
menu()
def Fahr():
''' Converts selected temperature from Fahrenheit to Celsius, Kelvin, or both.'''
selection = input('Convert to (C)elsius, (K)elvin, or (B)oth? -> ')
to_cels = round((5/9) * (temp_num - 32), 1)
to_kelv = round(5/9 * (temp_num - 32) + 273, 1)
if selection == 'C' or 'c':
print(temp_num, 'degrees Fahrenheit =', to_cels, 'degrees Celsius.\n')
exitapp()
elif selection == 'K' or 'k':
print(temp_num, 'degrees Fahrenheit =', to_kelv, 'degrees Kelvin.\n')
exitapp()
elif selection == 'B' or 'b':
print(temp_num, 'degrees Fahrenheit =', to_cels, 'degrees Celsius and', to_kelv, 'degrees Kelvin.\n')
exitapp()
else:
print('Please make a valid selection')
Fahr()
I have 2 other functions for Celsius and Kelvin. You’ll see the problem with my results in shell.
Here is what I get:
Enter a temperature to convert:
(ex. 32F) -> 32C
Convert to (C)elsius, (K)elvin, or (B)oth? -> b
32 degrees Fahrenheit = 0.0 degrees Celsius.
Exit the program?
Enter "y" or "n":
Without fail it always converts from Fahrenheit to Celsius. Every time.
You can’t abbreviate conditions like you have done. This:
Is the same as this:
Each side of
orandandare evaluated independently, and'f'by itself is always true. You need to use full phrases for each condition.