TypeError:无法连接'str'和'float'对象
我已经搜索并找不到如何解决这个问题,或者如何解决它。任何帮助表示赞赏,谢谢。TypeError:无法连接'str'和'float'对象
我的代码如下:
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”:
请输入一个整数,其后由单个空间然后或者华氏,C A F为摄氏,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
>>>
raw_input
全部更换temperature[:-1]
返回用户输入的string
。你在找什么是float
。在Python中,无法将string
和float
对象一起添加。所以,你将不得不做的第一个输入转换为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"
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
。
你改进的代码,用temp
更换temperature[:-1]
与unit
更换temperature[-1]
(它总是良好的编程习惯,以创建变量,而不是计算值多次):
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()
你需要的只是2点的变化:
1:刚raw_input
后
添加temperature=float(temperature.split(' ')[0]),temperature.split(' ')[1]
为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[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()
我可以知道为什么投票下来吗? – namit 2013-03-26 04:09:57
打开字符串转换为浮动或漂浮到这取决于期望的字符串。 – 2013-03-26 03:58:42
请修复您的缩进。如果他们没有立即看到这些块,这是可以理解的,但对他人无益。 – 2013-03-26 04:00:51