Unit 5 Flashcards
By the end of this unit, students will be able to: Create and use functions to organise programs into reusable chunks of code. Apply the concept of variable scope and how to access variables appropriately both inside and outside of functions
What is the purpose of function?
In order to save time and reuse code. Like many programming languages, Python doesn’t want you to type the same code over and over again.
Is print a function?
Yes
What is a function?
A function is a set of commands that’s been saved to be used again and again. it is a reusable block of code that you can use over again in your program without having to retype every line.
What are the three different parts to a function?
First part: A name
Second part: Parameters
Third part: A body.
What does the part of the function “name” do?
Just like with variables, you want to give your function a name that lets you know exactly what it does.
What is the meaning of “def”?
It is the first part of the function. It is short for define, which tells Python that we’re going to start writing a function.
What is the purpose of the “name” part of a function?
It tells Python that we’re going to start writing a function.
What is the purpose of the “parameters” part of a function?
Parameters let you give the function, information to work with.
Can a function have more than one parameters?
Yes, they are separated by commas.
Is this statement true or false?
You can create functions with no parameters or one parameter or as many parameter as you would like.
True.
It just depends on how many you need to make your function work.
What is the function of the third part of the function “body”?
It is to make the function do something
What should the third part of the function “body” have?
Your body has to have an indentation (the extra white part) before it so that Python knows it’s part of the function definition.
When you enter a function into the console (remembers the one with the prompt of arrows»>), IDLE will automatically ______________.
When you enter a function into the console (remember, the one with the prompt of arrows»_space;>), IDLE will automatically ident the next line for you after your “def” line,
How do you stop writing a function?
To stop writing a function, press enter twice to get the prompt (»>symbol) back.
How do you get out of defining your function if you write a function outside of the console?
When you write a function outside of the console (to save and submit for an assignment for instance), IDLE will add the tab and you will need to use the backspace or delete key to stop indenting and get out of defining your function.
What is the short cut to create a new file?
Control + N or command + N will create a new file.
What is the short cut that will run your file?
F5 key
We’ve learned how to send information to our function using parameters, but what if we want to receive information back from it?
We use the word “return” in our code for that.
def calculate allowance (allowance_per_chore, number_of_chores): (by right should be in one line)
return allowance_per_chore * number_of_chores
What are the three ways that we can code to continue this program if let’s say you did 5 chores and each allowance per chore is $2?
First, we can just print out the result
print (calculate_allowance(2.5))
10
Or
The other thing we can do is assign the result to another variable.
allowance= calculate_allowance (2,5)
print(allowance)
Or
Use another variable to store our information and then use those as parameters for our function
mark_allowance_per_chore = 2
mark_number_of_chores= 5
kate_allowance_per_chore =3
kate_number_of_chores = 4
calculate_allowance (mark_allowance_per_chore , mark_number_of_chores)
Output: 10
calculate_allowance (kate_allowance_per_chore , kate_number_of_chores)
Code this program:
To update our hours to seconds function to ask the user for input and then convert that number and return it.
def hours_to_seconds (hours):
print (hours * 60 *60)
Let’s update it now to ask for user input. We need to make sure that we change the entry to an integer or else our function won’t work .
def hours_to_seconds ():
hours= int(input(“Hours:”))
return hours * 60 *60
Now instead of printing out the seconds, we return the value, Let’s try using our function by printing out the results.
print (hours_to_seconds_())
Hours: 3 (user input)
10800
We successfully used our function to ask the user how many hours they would like to convert and then we returned the value in seconds and printed it out!
Write a program that prompts the user to enter three values: product name, product price, and quantity, and store these values into appropriate variables. Calculate the total price by multiplying products price by the quantity, Print the total price with an appropriate label as displayed above.
Enter product name: notebooks
Enter price: 1.99
Enter quantity: 8
8 notebooks cost 15.92
Get user input
product_name = input(“Enter product name:”)
price=float(input(“Enter price:”))
quantity=int(input(“Enter quantity:”))
#Calculate total price
total_price = price * quantity
# Print out results
print(“{} {}cost {}”.format(quantity, product_name, total_price))
Get this as output:
Enter product name: notebooks
Enter price: 1.99
Enter quantity: 8
8 notebooks cost 15.92
Extend the previous activity, this time calculating and printing the total price in formatted output using a function.
Prompt the user to enter: product name, price, and quantity, in that order.
Define a function to calculate and print total price
def calculate_and_print_total(name, price, qty):
#Calculate total price
total_price = price * quantity
#Print output
print(“{} {} cost {}”.format(qty, name, total_price))
#Get user input
product_name = input(“Enter product name:”)
product_price = float(input(“Enter price:”))
quantity= int(input(“Enter quantity”))
#Call the function
calculate_and_print_total(product_name, product_price, quantity)
What do we do if we want to receive information back from a function?
We use the function “return”.
Code something that get this as the output
Enter product name: notebooks
Enter a price: $1.99
Enter quantity: 8
8 notebooks cost $15.92
Extend the previous activity again, this time returning the total price from a function and print the formatted output from your main program.
Define a function to calculate total price
def calculate_total(price, qty):
#Calculate and return total price
return price * qty
#Get user input
product_name = input(“Enter product name:”)
product_price= float(input(“Enter a price: $”))
quantity = int(input(“Enter quantity:”))
#Call the function
total_price = calculate_total(product_price, quantity)
#Print results
print(“ {} {} cost ${}”.format(quantity, product_name , total_price))