Word Counter In python -
i have tried create word counter in python, code fails run.
word = input("enter word or sentence") input("your word/ sentence has"+ len(word) + " letters") could please me fix problem?
the current result
typeerror: can't convert "int" object str implicity
you try below code:
word = input("enter word or sentence") input("your word/ sentence has"+ str(len(word)) + " letters") here, i'm using str(len(word)) instead of len(word). because len(word) returns number , it's int object.
and you're doing str_object + int_object, , python doesn't understand want do.
let's see:
>>> len('foobar') 6 >>> type(len('foobar')) <class 'int'> >>> len('foobar') + 'foobar' traceback (most recent call last): file "<input>", line 1, in <module> typeerror: unsupported operand type(s) +: 'int' , 'str' >>> so have convert int_object (which returned len(word)) str object use str() function.
for example:
>>> str(len('foobar')) '6' >>> type(str(len('foobar'))) <class 'str'> >>> str(len('foobar')) + 'foobar' '6foobar' you can use str.format() instead of 2 +. can auto convert objects str, it's more readable code.
so use:
word = input("enter word or sentence") input("your word/ sentence has {} letters".format(len(word)))
Comments
Post a Comment