Search code examples
pythoninputtypeerror

type error while multiply using float value


I am trying to multiply user input by float value. Below is my code

user_input = input("enter your float number:-")
raw = user_input * 2.2
print (raw)

but getting following error message:

Traceback (most recent call last):
  File "/home/pravin/Documents/python_app_build_project/section_5/test1.py", line 3, in <module>
    raw = user_input * 2.2
          ~~~~~~~~~~~^~~~~
TypeError: can't multiply sequence by non-int of type 'float'
i want to convert user_input by 2.2

Solution

  • user_input is of type str, you can check it by doing this:

    user_input = input("enter your float number:-")
    print(type(user_input))
    

    You shoud convert user_input to numeric type before performing multiplication, for example float(user_input) * 2.2

    But be prepared to catch an exception if user enters something other than a float number, for example, a word - automatic conversion to float will fail in that case.