[英]What is the wrong withe code python
当我在http://www.pyschools.com/quiz/view_question/s2-q1 中执行此代码时。 它给出了两个答案的错误......请帮助问题是:
Write a function to convert temperature from Celsius to Fahrenheit scale.
oC to oF Conversion: Multipy by 9, then divide by 5, then add 32.
Examples
>>> Cel2Fah(28.0)
'82.40'
>>> Cel2Fah(0.00)
'32.00'
# Note: Return a string of 2 decimal places.
def Cel2Fah(temp):
x = 9.00
y = 5.00
z = 32.00
a = temp * x
a = a / y
a = a + z
return(a)
上面写着:
# Note: Return a string of 2 decimal places.
您没有返回字符串。 您正在返回一个float
类型的值。
由于这看起来像是家庭作业,我会让你弄清楚如何解决这个问题(提示:使用字符串格式化操作符)。
改为返回这个。
return '%.2f' %a
将您的退货声明更改为阅读
return '%.2f' %a
该代码在语法上没有任何问题。 如果您将数字作为参数传递,它应该可以工作。 如果你只传递一个数字,它就不会起作用。
def Cel2Fah(temp):
cel=(round((((temp*9)/5) +32),2))
return '%.2f' %cel
print Cel2Fah(36.9)
print Cel2Fah(29.0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.