Avoiding Error Messages
Error Messages Related to Input Arguments. One of the most common error messages you’ll encounter is when you call a function that has required arguments, but you don’t pass the arguments in the function call. Here you call
add_item() without passing the required arguments
quantity. You’ll get a
TypeError whenever a required argument is missing.
00:27 The error message is a helpful one in this case. Error messages are not always as helpful as this one. However, missing required parameters are not the only error message you’ll encounter as you learn to define functions with required and optional parameters.
00:45 When none of the parameters in a function definition has default values, you can order the parameters in any way you wish. The same applies when all the parameters have default values. However, when you have some parameters with default values and others without, the order in which you define the parameters is important.
01:19 Depending on the editor that you use, it may highlight this issue without you even needing to run the code. You may have noticed that the editor on-screen has highlighted that there’s an error present. It’s still possible to run the script, however, and the error message that you get when you run this code explains the rule fairly clearly.
The parameters with no default value must always come before those that have a default value. In the example you’ve just seen,
quantity must always be assigned a value as an argument.
01:52 Placing parameters with default values first would make the function call ambiguous. The first two required arguments can then be followed by an optional third argument. In the next section of the course, you’ll see how to create functions that can accept any number of arguments.
Become a Member to join the conversation.