Luke Livingstone
Luke Livingstone

Reputation: 129

Unsupported operand type(s) for +: 'dict' and 'int'

Bit of a weird one!

Could anyone shed some light on this TypeError message?

csvRow.append(len(calls["outbound"] + len(calls["inbound"])))
TypeError: unsupported operand type(s) for +: 'dict' and 'int'

When I do the following, I get no issues and it runs as expected:

totalinbound = len(calls["inbound"])
totaloutbound = len(calls["outbound"])
csvRow.append(totalinbound + totaloutbound)

Upvotes: 1

Views: 18762

Answers (2)

cwallenpoole
cwallenpoole

Reputation: 82038

You have a typo.

# calculates the len of (dict + len of dict)
len(calls["outbound"] + len(calls["inbound"]))

# calculates the len of dict + len of dict
len(calls["outbound"]) + len(calls["inbound"])

Upvotes: 2

user2555451
user2555451

Reputation:

Your parenthesis are not balanced correctly. calls["outbound"] should be inside the parenthesis that call the len function:

csvRow.append(len(calls["outbound"]) + len(calls["inbound"]))
#                                  ^

I moved a closing parethesis from the end of the line to where the arrow is.

Otherwise, you will be trying to add len(calls["inbound"]) with the dict returned by calls["outbound"]. This is a TypeError.

Upvotes: 7

Related Questions