python3中转换温度的小程序
我正在尝试创建一个程序,其中用户输入的温度可以根据用户的不同转换为华氏度或摄氏度。从摄氏度到华氏度的转换工作正常。然而,从华氏到摄氏,它给出了一个十六进制的答案 我试图移动括号和公式,但似乎没有给出预期的结果python3中转换温度的小程序,python,python-3.x,Python,Python 3.x,我正在尝试创建一个程序,其中用户输入的温度可以根据用户的不同转换为华氏度或摄氏度。从摄氏度到华氏度的转换工作正常。然而,从华氏到摄氏,它给出了一个十六进制的答案 我试图移动括号和公式,但似乎没有给出预期的结果 #!/usr/bin/python3 def fahrenheit(c): fahrenheit=c *9/5+32 print (fahrenheit) def celsius(f): celcius=5/9*(f-32) print (celsius) #f= int (
#!/usr/bin/python3
def fahrenheit(c):
fahrenheit=c *9/5+32
print (fahrenheit)
def celsius(f):
celcius=5/9*(f-32)
print (celsius)
#f= int (input("Please enter the temperature in fahrenheit"))
#c= int (input("Please enter the temperature in celcius"))
conversion= (input("Please enter which measurement to convert to fahrenheit or celsius"))
if conversion == "fahrenheit":
c= int (input("Please enter the temperature in celcius"))
fahrenheit (c)
elif conversion == "celsius":
f= int (input("Please enter the temperature in fahrenheit"))
celsius (f)
else:
print ("Please enter the appropriate operator ")
华氏温度到摄氏度的输出:
<function celsius at 0x7f539a933840>
您需要指定不同的变量名,这些变量名与函数名不冲突。
返回
返回值,而不是将返回值留给解释器也很好。如链接答案中所述,变量名不应与函数名冲突。并且,正如答案中所建议的,返回计算值,而不是在函数本身中打印它
我想补充几点
- 给出有意义的名字。例如,在将温度转换为华氏温度时,不要将函数命名为
fahrenheit
,而是将其命名为
- 当询问用户要转换什么时,不要期望他们写出完整的单词(华氏或摄氏),而是给他们一些简单的选项,如f或c
非常感谢,将变量从函数名更改为有效。谢谢,我按照您的建议更改了函数名,这有助于提高其可读性。