TypeError: can't multiply sequence by non-int of type 'float' python 2.7

非 Y 不嫁゛ 提交于 2019-12-12 00:26:28

问题


Hi I'm a 11 year old who has taken up python as a hobby. I'm trying to make a mass converter as a first project. But for some reason I've been getting this error: TypeError: can't multiply sequence by non-int of type 'float'

Here is my code:

    print "please enter the amount of kilograms you want to convert",
    kilo = raw_input() 
    pounds = 2.20462

    print kilo * pounds

回答1:


raw_input returns a string, you're basically doing this:

print "1234" * 2.20462

You need to convert the input to a number:

kilo = float(raw_input())
pounds = 2.20462

print kilo * pounds

 

The error message is somewhat confusing because you can multiply a string (or any sequence) by an integer:

print "abc" * 3   # prints "abcabcabc"


来源:https://stackoverflow.com/questions/16268935/typeerror-cant-multiply-sequence-by-non-int-of-type-float-python-2-7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!