将原始输入变量除以numb时出现Python TypeError

2024-10-04 09:22:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把输入的磅重量转换成公斤,我得到以下错误。。。在

TypeError: unsupported operand type(s) for /: 'unicode' and 'float'

我的代码:

lbweight = raw_input("Current Weight (lb): ") 

kgweight = lbweight/2.20462

有人请帮忙!在


Tags: and代码forinputrawtype错误unicode
3条回答

这是因为使用raw_input,输入是raw,表示一个字符串:

lbweight = float(raw_input("Current Weight (lb): ") )

kgweight = lbweight/2.20462

raw_input返回一个字符串,应使用float()将输入转换为float:

float(raw_input("Current Weight (lb): "))

注意错误消息TypeError: unsupported operand type(s) for /: 'str' and 'float'

>>> kgweight = lbweight/2.20462

Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    kgweight = lbweight/2.20462
TypeError: unsupported operand type(s) for /: 'str' and 'float'
>>> 

如果2.20462是一个浮点数,那么这里哪一个是字符串?文档对raw_input说了些什么?在

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.

相关问题 更多 >