Fixing Errors

Challenge 5-1: Debugging

How can we fix this error? We know that concatenation won’t work.

>>> "friend" + 5

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

TypeError: Can't convert 'int' object to str implicitly

What if we make the 5 a string?

>>> "friend" + "5"

'friend5'

What's another way that we could fix this error? What if we multiply instead of concatenating?

>>> "friend" * 5

'friendfriendfriendfriendfriend'

Let's do something new - let's try the print command:

>>> print("friend", 5)

friend 5

When we use the print command, we can print multiple things, we just need to use commas to separate them.

There are many more errors than just the TypeError we've shown here. For example, in Challenge 3-1 we saw the IndexError which lets you know if you've tried to access an element of a list that doesn't exist.

Thankfully, Python's error messages are usually really descriptive, and tell you what's going wrong. All error messages also include a line number, which tells you which line of code has given you the error. This lets you look back at your code, at the specific line that's going wrong, and figure out what's happening!