Introduction
Good work making it through your first lab! We will apply those skills in this lab and learn new ones as well.
Just like the first lab, this lab is very important for learning the foundations of Python. This lab is also a good tool to look back at, so make sure you understand the concepts!
In this lab you will learn:
- defining functions
- returning values
- calling functions
- parameters
Provided Code
You will notice we provided two functions for you. main and run. You should not modify anything in the run function (unless we tell you to), but we encourage you to read through it. The run function is the ‘glue’ in the Divide-Conquer-Glue strategy.
Run Your Program (Step 0)
Start by running your program in ‘Develop Mode’. This won’t appear to do anything because you haven’t written any code yet! It should say:
(Your program produced no output)
REMINDER: You should be running your code as you work - just to see what is happening after each step.
Print statement (Step 0.5)
Find the printStatement function. This function has already been written for you but you should make sure that you understand what is happening within the function before completing the following steps.
The function signature for the function is:
def printStatement():
The keyword def is used to define a function printStatement is the name of the function.
Here are some facts about functions:
- Functions are used to keep your code organized. Functions usually do one thing at a time.
- You can name functions whatever you want as long as you don’t use invalid characters. It’s good practice to name your function based on what the function does.
Inside the function the following occurs:
print("Welcome to your second lab!")
Remember how when you ran your program there was no output? This is because the call to the function was commented out. Comments are signified with a #; anything with a # in front is ignored by the compiler. Go down to the run function and get rid of the # in front of printStatement().
Note: The call to function is an important part of testing your code to make sure you are getting the output you want. You will be expected to code your own call to functions in future labs.
Note: Make sure to keep printStatement() indented in the run function. Indentation is a big part of Python that you will continue to learn about.
Now run your program again. If see the output Welcome to your second lab! then you’ve done this correctly! Now we can move on to the next step.
Creating your own print statement (Step 1)
We will continue with the printStatement function. Under the print(“Welcome to your second lab!”), print “I’m learning about functions!”. Make sure it is indented underneath the function at the same indentation level as the other print statement. Run your program and make sure you have the correct output! It should look like this:
Welcome to your second lab!
I'm learning about functions!
Returning Values (Step 2)
Find the returnValues function. It’s very similar to the printStatement function except we are returning “Welcome to your second lab!” instead of printing.
Go down to the run function and uncomment out returnValues(), then run your program. Notice how nothing changes from your last run? This is because the value WAS returned, but it was not printed. In order to show that returned value, you need to print out the call to the function. Add a print statement to encapsulate returnValues(), it should look like this:
print(returnValues())
Now rerun your code and now the output should be:
Welcome to your second lab!
I'm learning about functions!
Welcome to your second lab!
Returning Values Part 2 (Step 3)
Let’s try returning our own value now. Unlike print statements, we can only have one return statement per function (in most cases). Let’s change that return line to say ‘This is my first return statement!’
Output should now look like this:
Welcome to your second lab!
I'm learning about functions!
This is my first return statement!
Parameters (Step 4)
Find the parameters function. It looks like this:
def parameters(num1,num2):
multiplication = num1 * num2
return multiplication
Notice how this function is defined a little differently. There are variables in the parentheses. These are called parameters and are essentially placeholders. Parameters are used so that you can call the function multiple times using different values each time.
Go down to run() and uncomment print(parameters(1,4))
Your output should now look like this:
Welcome to your second lab!
I'm learning about functions!
This is my first return statement!
4
Below print(parameters(1,4)), create another call to the parameters function using the numbers 5 and 7.
Output should look like this:
Welcome to your second lab!
I'm learning about functions!
This is my first return statement!
4
35
Hint: If you are having trouble with this, copy the example given to you, and replace 1 and 4 with 5 and 7.
Putting it all together (Step 5)
In this step we will be combining everything we’ve learned so far.
- First you will need to define a function called iceCreamOrder. Go to step 0.5 to learn about defining a function if you don’t remember how to.
- Within that function, create an int variable called cost and assign it to equal 6.
- Then ask the user for input asking “What flavor would you like? => “ and store their answer in a variable called flavor. Hint: If you don’t remember user input, look back at lab 00.
- Next return a statement stating “The flavor will cost cost dollars.” replacing flavor and cost with your two variables you created.
Hint: Follow the same format as how you coded your print statement in lab 00.
Hint: Use casting to cast cost from an int to a string or else it won’t return properly. (An error will occur)
In the zyBooks input box, put ‘vanilla’ to test your code.
Don’t forget to call the function to see the output. Make sure to put it above the return in run().
If you do this successfully, the output should look like:
Hello World!
This is my first print statement!
This is my first return statement!
4
35
What flavor would you like? => The vanilla will cost 6 dollars.
Nice work, you just finished the lab! Now you can run in ‘Submit Mode’ to make sure you get full points.
Make sure to check the next step below to make sure your grade is sent to Canvas.
Turning In (Step 6)
Make sure you click through the canvas link to the assignment if you haven’t already. This triggers the linking process, so canvas can get an updated score. As with the previous assignment, you only have five changes to ‘submit for grading’, so it is important to run the program testing it for various cases before you click submit for grading.