TypeError:无法连接'str'和'float'对象

时间:2013-03-26 03:52:04

标签: python typeerror

我已经搜索过,无法弄清楚这个问题是如何产生的,或者如何解决它。感谢任何帮助,谢谢。

我的代码如下:

def main():
temperature = raw_input("Please enter an integer followed by a single space then either a F for Fahrenheit, C for Celsius, K for Kelvin, or R for Rankine: ")
    # algorithm for Farenheit to Kelvin, Celsius, and Rankine
    if temperature[-1] == "F":
    K = (temperature[:-1] + (459.67) * (5.0/9.0))
    print K
    C = (temperature[:-1] - 32) * (5.0/9.0)
    print C
    R = (temperature[:-1] + 459.67)
    print R
    # algorithm for Celsius to Kelvin, Fahrenheit, and Rankine
    elif temperature[-1] == "C":
        K = (temperature[:-1] + 273.15)
        print K
        F = (temperature[:-1] * (9.0/5.0) + 32)
        print F
        R = (temperature[:-1] + 273.15) * (9.0/5.0)
        print R
    # algorithm for Kelvin to Celsius, Fahrenheit, and Rankine
    elif temperature[-1] == "K":
        C = (temperature[:-1] - 273.15)
        print C
        F = (temperature[:-1] * (9.0/5.0) - 459.67)
        print F
        R = (temperature[:-1] * (9.0/5.0))
        print R
    # algorith for Rankine to Fahrenheit, Celsius, and Kelvin
    elif temperature[-1] == "R":
        F = (temperature[:-1] - 459.67)
        print F
        C = (temperature[:-1] - 491.67) * (5.0/9.0)
        print C
        K = (temperature[:-1] * (5.0/9.0))
        print K
main()
输入“50 F”后

和我的错误信息:

请输入一个整数后跟一个空格,然后输入F表示华氏度,C表示摄氏度,K表示开尔文,或R表示朗肯:50 F

Traceback (most recent call last):
  File "/Users/Minotza/Documents/multi-unit_temperature_converter.py", line 36, in <module>
    main()
  File "/Users/Minotza/Documents/multi-unit_temperature_converter.py", line 5, in main
    K = (temperature[:-1] + (459.67) * (5.0/9.0))
TypeError: cannot concatenate 'str' and 'float' objects
>>> 

3 个答案:

答案 0 :(得分:3)

raw_input返回string个用户输入。您要找的是float。在Python中,无法将stringfloat对象添加到一起。因此,您需要做的是将第一个输入转换为float,以便您可以对其执行数学运算。

以下是我将如何做到这一点:

user_input = raw_input("Prompt Text...").split(' ')

temperature = float(user_input[0])
units_or_option = user_input[1]

因此,split方法根据用户输入的内容列出一个列表,使用空格字符来分割条目。 temperature包含空格前的第一个内容,即floatunits_of_option包含在数字和空格后输入的字符。

因此,如果用户输入:

123.5 F

变量的值为:

user_input = ["123.5", "F"]
temperature = 123.5
units_or_option = "F"

答案 1 :(得分:2)

您只需要2次更改:
第一
temperature=float(temperature.split(' ')[0]),temperature.split(' ')[1] raw_input之后添加temperature 例如:

temperature = raw_input("Please enter an integer followed by a single space then either a F for Fahrenheit, C for Celsius, K for Kelvin, or R for Rankine: ")
temperature=float(temperature.split(' ')[0]),temperature.split(' ')[1]
##. . . . your code


第二
并将所有temperature[:-1]替换为temperature[0]



你的最终代码

def main():
    temperature = raw_input("Please enter an integer followed by a single space then either a F for Fahrenheit, C for Celsius, K for Kelvin, or R for Rankine: ")
    # algorithm for Farenheit to Kelvin, Celsius, and Rankine
    temperature=float(temperature.split(' ')[0]),temperature.split(' ')[1]
    if temperature[-1] == "F":
        K = (temperature[0] + (459.67) * (5.0/9.0))
        print K
        C = (temperature[0] - 32) * (5.0/9.0)
        print C
        R = (temperature[0] + 459.67)
        print R
    # algorithm for Celsius to Kelvin, Fahrenheit, and Rankine
    elif temperature[-1] == "C":
        K = (temperature[0] + 273.15)
        print K
        F = (temperature[0] * (9.0/5.0) + 32)
        print F
        R = (temperature[0] + 273.15) * (9.0/5.0)
        print R
    # algorithm for Kelvin to Celsius, Fahrenheit, and Rankine
    elif temperature[-1] == "K":
        C = (temperature[0] - 273.15)
        print C
        F = (temperature[0] * (9.0/5.0) - 459.67)
        print F
        R = (temperature[0] * (9.0/5.0))
        print R
    # algorith for Rankine to Fahrenheit, Celsius, and Kelvin
    elif temperature[-1] == "R":
        F = (temperature[0] - 459.67)
        print F
        C = (temperature[0] - 491.67) * (5.0/9.0)
        print C
        K = (temperature[0] * (5.0/9.0))
        print K
main()

答案 2 :(得分:1)

temperature = raw_input("Please enter an integer followed by a single space then either a F for Fahrenheit, C for Celsius, K for Kelvin, or R for Rankine: ")

raw_input返回一个字符串,因此您希望拆分字符串或将其转换为每一步浮动。

将数字设置为变量而不是重复计算总是更好,所以你可以做到

将其转换为浮点数:

temperature = raw_input("Please enter an integer followed by a single space then either a F for Fahrenheit, C for Celsius, K for Kelvin, or R for Rankine: ")
temp = float(temperature.split(' ')[0])

string.split(obj)所做的是通过将string拆分为obj来返回273 K部分的列表。例如,如果用户输入了temperature == '273 K',因此temperature.split(' '),则['273', 'K']通过在每个空格中分割列表来创建列表,该列表变为float(temperature.split(' ')[0]) # we only want to convert the first element。但是,这些仍然是字符串,所以我们希望将第一个转换为浮点数,因此我们执行temperature[:-1]

您改进的代码,将temp替换为temperature[-1]并将unit替换为def main(): temperature = raw_input("Please enter an integer followed by a single space then either a F for Fahrenheit, C for Celsius, K for Kelvin, or R for Rankine: ") # algorithm for Farenheit to Kelvin, Celsius, and Rankine temp, unit = float(temperature.split(' ')[0]), temperature[-1] # multi-assignment if unit == "F": K = (temp + (459.67) * (5.0/9.0)) print K C = (temp - 32) * (5.0/9.0) print C R = (temperature[:-1] + 459.67) print R # algorithm for Celsius to Kelvin, Fahrenheit, and Rankine elif unit == "C": K = (temp + 273.15) print K F = (temp * (9.0/5.0) + 32) print F R = (temp + 273.15) * (9.0/5.0) print R # algorithm for Kelvin to Celsius, Fahrenheit, and Rankine elif unit == "K": C = (temp - 273.15) print C F = (temp * (9.0/5.0) - 459.67) print F R = (temp * (9.0/5.0)) print R # algorith for Rankine to Fahrenheit, Celsius, and Kelvin elif temperature[-1] == "R": F = (temp - 459.67) print F C = (temp - 491.67) * (5.0/9.0) print C K = (temp * (5.0/9.0)) print K main() (它总是更好的编程习惯来创建变量而不是计算价值多次):

{{1}}