Functions with Parameters and Return Values (Chapter 8)
Subsection12.9.1Level 1
In this level you will create a function that will take in 3 floats and return the average value of the floats.
The starter code below creates three user input variables (floats), asking the user to enter a decimal number (note that we donβt use the word float in the prompt - end users donβt know what floats are!). Create a function header for a function that takes in three parameters and will calculate and return their average. Name the function something that makes sense and reflects what it will do. This function expects floating point numbers and returns a floating point number - make sure your header includes appropriate type annotations.
Inside the function, create a variable and assign to it the average value of the three passed in floats. Note: To calculate an average you take the sum of inputs and divide it by the number of inputs.
After the line that calculates and stores the average in a variable, return that variable.
At the bottom of the script, call your newly defined function, passing in the three numbers. Assign the result of calling the function to a new variable.
Finally, create a print statement that includes the following: βThe average is,β then the average.
Below is an example of the output, after a user enters 56.89, 23.45, and 98.33
Subsection12.9.2Level 2
In this level you will create a turtle program that will draw a whole bunch of squares that vary in size and colour. You are given starter code with a list of colours in it, and a turtle and a window. Follow the steps.
The starter code above contains two functions: the main function that creates the turtle and the world, and then has a for loop that iterates 10 times, and the move_random function that moves the turtle to random coordinates. Run the code to see what it does.
Note that on line 30, we call the move_random function and pass the turtle. The move random function has a turt parameter, and when it is called, that turt paramter is a reference variable that points at the turtle we created (dorsa). Inside the move_random function, when we give a turtle command we use the name βturtβ, but it is actually a reference to our dorsa turtle and causes dorsa to pick up her pen, move, and then put down her pen. Note also, that this means the move_random function can be called with another turtle, and it works no matter what name you give your turtle.
Look at line 34 in main. This is a commented out function call. Itβs commented out because the square function doesnβt exist yet. Your job is to create a square function that takes three parameters: a turtle, a number representing the size of square to draw, and a color. Define this function at the top of the script where you see the #TODO. You can choose whatever parameter names you want (itβs fine to use βturtβ for the turtle, just like in the move_random function). Inside this function, change the turtle to the color passed in, then write the code to draw a square, using the size value.
Uncomment the square function call in main to test your function.
Add type annotations to your function and to the move_random function.
Subsection12.9.3Level 3
In this level you will write a function that takes in a string, and returns the number of vowels found in the string. Follow the steps.
Ask the user to enter a word or phrase and save their response to a string variable.
Define a new function called βcount_vowelsβ. This function should take a single parameter (you will pass in the string you just got from the end user). The function will return the number of vowels in the string. Add appropriate type annotations.
Inside the function:
Create a counter variable and initialize it to 0.
Write a for loop that iterates across the string parameter
For each character in the string, see if it is equal to βaβ, or βeβ or βiβ or βoβ or βuβ.
If it is equal to a vowel, increment the counter variable. Do you need an else block?
After the for loop, add a line that returns the counter variable.
Call the count_vowels function. You need to pass in the userβs response as a parameter. You also need to assign the return value to a variable.
Print out the result to the console.
Once this is working, create a loop that iterates 3 times. Put the code that asks the user for a phrase and calls the count_vowel function inside that loop, so that we see the function getting executed multiple times with different values.