I'm trying to learn python for data science application and signed up for a course. I am doing the exercises and got stuck even though my answer is the same as the one in the answer key.
I'm basically trying to add two new items to a dictionary with the following piece of code:
# Create a new key-value pair for 'Inner London'
location_dict['Inner London'] = location_dict['Camden'] + location_dict['Southwark']
# Create a new key-value pair for 'Outer London'
location_dict['Outer London'] = location_dict['Brent'] + location_dict['Redbridge']
but when I run it I am getting a TypeError
.
The Python "TypeError: unsupported operand type(s) for /: 'str' and 'int'" occurs when we try to use the division / operator with a string and a number. To solve the error, convert the string to an int or a float , e.g. int(my_str) / my_num .
The TypeError: unsupported operand type(s) for +: 'int' and 'str' error occurs when an integer value is added with a string that could contain a valid integer value. Python does not support auto casting. You can add an integer number with a different number. You can't add an integer with a string in Python.
The Python "TypeError: unsupported operand type(s) for +: 'int' and 'str'" occurs when we try to use the addition (+) operator with an integer and a string. To solve the error, convert the string to an integer, e.g. my_int + int(my_str) .
I had a list in there with the sets I think that was what was throwing the error I just converted the list to a set and joined them with .union. Thank you @Adelin, @Bilkokuya, @ Piinthesky, and @Kurast for you're quick responses and input!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With