These function tasks are easier than the ones in the other function checklist.
1a) Define a function that receives a number, n and returns 3n + 5. Do not use the print() command anywhere in this function.
(tip: use * to multiply 3*n to avoid a syntax error)
1b) Run your program. (Nothing happens yet, but there should be no errors)
1c) Call your function in the body of your program. Not inside the function. (Nothing happens yet, because you didn’t tell it to print anything)
1d) Print the function three times using three different input numbers.
1e) Save your function’s result as a variable. Then print the variable to make sure that the value was returned and saved properly.
Example of saving your function’s result into a variable:
x = my_function(a)
After doing that, you can print x and it should contain the return value of my_function.
For all the rest of this checklist, you need to define and call the function just like we did in the first example, and you should not be printing the result of the function inside the definition. Return the answer instead and print it from the main program.
2) Define a function that receives the price of an item and returns the total after adding 6% sales tax.
For example, if the function’s input price is 2.00, the function should return a total of 2.12. The return value should be a float; do not include a dollar sign.
3) Define a function that receives a word variable, w, as an input. This function needs to return a string that puts the word in a sentence as follows: “the word {insert word here} is {insert number here} characters long.” (Use the len() function to find how many characters are in the sentence)
Tip: convert the len() to a str(). Example: str(len(x))
4) Define a function that receives two numbers, a and b. Use if/else logic, and return the following results:
If a is greater than b, return 1.
If b is greater than a, return -1.
If a equals b, return 0.
(This style of function can be useful in a sorting algorithm.)
5) Create a function that calculates acceleration from force and mass using Newton’s 2nd Law of motion, which can be written as follows:
a = F/m
If mass is zero or negative, return “None” instead of returning a number. The equation only makes sense if mass is a positive amount. None is the same thing as Null in other languages such as Java. It means there is no result, and the function would not be equal to anything in that case.
Test the function for inputs that are positive, negative, and zero to verify that it works as intended in each case.
6) Create a program that calculates a total price for some widgets.
There is a bulk pricing discount. The normal price of a widget is $9.95. However, if you buy at least 10 widgets, you get them for $7.95 each.
Create this function, and test it for different input values to make sure they all work properly.