user1874409
user1874409

Reputation: 1

How to set variable type? TypeError: cannot concatenate 'str' and 'int' objects

I've looked at many similar issues but cannot find an answer so I'm hoping you guys can help me. I keep getting that TypeError message but have no idea why. Any advice would be greatly appreciated.

number = raw_input("Enter a number to be rounded: ")

decimallocation = number.find('.')
right = number[decimallocation:]    
greater = int(number+right+1)
lesser = int(number+right)

if right >= .5:
    print (greater)
else:
    print (lesser)

Upvotes: 0

Views: 262

Answers (2)

IT Ninja
IT Ninja

Reputation: 6430

You need:

greater=int(int(number)+int(right)+1)
lesser=int(int(number)+int(right))

You need to make sure everything you are adding is an int or a str, you cant add both types together.

Upvotes: 0

Sailesh
Sailesh

Reputation: 26197

The error is this line:

greater = int(number+right+1)

I think what you are trying to do is:

greater = int(number+right) + 1

One more possible error is in checking:

right > .5

where right is a str but .5 is not

Upvotes: 1

Related Questions