我对Python非常陌生,但是对学习非常感兴趣。对于单Python课程,我将创建一个将温度转换为摄氏度或华氏度的程序,并将转换结果显示为小数点后第一位。我的问题在于我的印刷线。
如您在我的代码中看到的,我试图在连接时使用round()函数。尽管我尝试将将float转换为str(),但在运行程序时却遇到以下错误。
对于以下行
同样,对于我的elif bloc中的打印行。任何理解和解决问题的帮助将不胜感激。
temp=float(input("Enter a temperature value to convert: "))
unit=str(input("Convert to Fahrenheit or Celsius? Enter f or c: "))
if unit=="c" or unit == "C":
degC=(temp)
temp=(1.8*temp)+32
print (round(str(temp), 1) + " degrees fahrenheit = " + str(degC) + " degrees Celsius. ")
elif unit=="f" or unit == "F":
degF=(temp)
temp=(temp-32)/1.8
print (round(str(temp), 1)+ " degrees celsius = " + str(degF) + " degrees Fahrenheit. ")
else:
print("you did not enter an f or c. Goodbye ")
我的输出应四舍五入到小数点后第一位。
不要转换temp
为字符串。将其保留为浮动round
。将结果round
转换str
为串联后
str(round(temp, 1))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句