Python 3.4.1中“print”语句中的语法错误
我在Python3.4.1中遇到了一个语法错误,我不知道它是用来做什么的。我正试图编写一个程序,将摄氏度转换成华氏度,反之亦然。但是,在print语句的最后一个引号上出现了语法错误。有人知道这是什么原因吗Python 3.4.1中“print”语句中的语法错误,python,python-3.x,syntax-error,Python,Python 3.x,Syntax Error,我在Python3.4.1中遇到了一个语法错误,我不知道它是用来做什么的。我正试图编写一个程序,将摄氏度转换成华氏度,反之亦然。但是,在print语句的最后一个引号上出现了语法错误。有人知道这是什么原因吗 def celsiusToFahrenheit(celsius): """Converts celsius into Fahrenheit""" (9/5) * celsius + 32 def fahrenheitToCelsius(fahrenheit): """C
def celsiusToFahrenheit(celsius):
"""Converts celsius into Fahrenheit"""
(9/5) * celsius + 32
def fahrenheitToCelsius(fahrenheit):
"""Converts fahrenheit into celsius"""
(5/9) * (fahrenheit - 32)
celsius = 40
print ("40 degrees celsius is", celsiusToFahrenheit(celsius)
"in Fahrenheit")
你有:
print ("40 degrees celsius is", celsiusToFahrenheit(celsius)
"in Fahrenheit")
在每个项目之间添加逗号:
print ("40 degrees celsius is", celsiusToFahrenheit(celsius),
"in Fahrenheit")
用于分隔参数的函数调用中缺少逗号:
print ("40 degrees celsius is", celsiusToFahrenheit(celsius), # <= Here
"in Fahrenheit")
另外,你的下一个问题是为什么你不能从celsiusToFahrenheit那里得到你想要的。这是因为您不返回值。将每个函数的最后一行分别替换为返回9/5*摄氏+32和返回5/9*华氏-32。这是一种更具可读性和python风格的打印方式:
print ("40 degrees celsius is {0} in Fahrenheit".format(celsiusToFahrenheit(celsius)))
您可能还想从函数中返回值。我知道我还没有返回值,但我正在努力。谢谢你的提示。编辑:它起作用了!谢谢。