繁体   English   中英

Python浮动为String TypeError

[英]Python Float to String TypeError

我正在编写一个温度转换器来娱乐,除我收到“ TypeError:无法将'float'对象转换为隐式str'消息外,一切似乎都在工作。 从我可以看出来的事实来看,我正在将其转换为字符串,有人可以告诉我我做错了什么吗?

我以前看过这个问题“无法将'float'对象转换为隐式str“

def tempconverter(startType,Temp):
    #conversion types: c -> f c-> K , f -> c, f -> k, k -> c, k -> f
    # maybe have them select only their beginning type then show
    # all temp types by that temperature

if startType[0].lower() == 'c':
    return  ('Your temperature ' + Temp +'\n'
            + 'Farenheight: ' + ((int(Temp)*9)/5)+32 + '\n'
            + 'Kelvin: ' + str((Temp+ 273.15)) ## I get the error here ##
            )
elif startType[0].lower() == 'f':
    #commented out until first is fixed
    #return  ((int(Temp)-32)*5)/9
    return Temp
elif startType[0].lower() == 'k':
    return  Temp

print('Welcome to our temperature converter program.')
print('''Please enter your temperature type:
         C = Celsius
         F = Farenheight
         K = Kelvin''')
sType = input('> ')

print('Please enter the temperature you wish to convert.')
sTemp = input('> ')

print(tempconverter(sType,sTemp))

在代码行中:

+ 'Kelvin: ' + str((Temp+ 273.15)) ## I get the error here ##

该错误是由以下部分引起的:

Temp+ 273.15

Temp是一个字符串。 您不能将字符串和数字加在一起。

在此计算中:

((int(Temp)*9)/5)+32

您没有将结果转换为str

另外,您拼写的“华氏温度”错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM