2

我已经搜索过,无法弄清楚这个问题是如何出现的,或者如何解决它。任何帮助表示赞赏,谢谢。

我的代码如下:

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
>>> 
4

3 回答 3

3

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

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

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

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

因此,该split方法根据用户输入的内容制作一个列表,使用空格字符来拆分条目。temperature包含空格前的第一件事,转换为float. units_of_option包含在数字和空格之后输入的字符。

因此,如果用户输入:

123.5 F

变量具有以下值:

user_input = ["123.5", "F"]
temperature = 123.5
units_or_option = "F"
于 2013-03-26T04:06:36.890 回答
2

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

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()
于 2013-03-26T04:03:33.067 回答
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()
于 2013-03-26T04:02:59.643 回答