我认为解释这一点的最好方法是发布我的代码,以及来自 shell 的结果。我是 Python 新手(使用 3.3),而且一般都在编码,所以我确定我只是错过了一些非常简单的东西。
这是我的代码:
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()
对于摄氏温度和开尔文温度,我还有另外 2 个功能。你会在 shell 中看到我的结果的问题。
这是我得到的:
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":
毫无疑问,它总是从华氏温度转换为摄氏温度。每次。