Using Functions with Arguments in Python
In this activity, you will get more practice with functions. Functions are a great way to wrap chunks of code so you can use them later. Functions take in an argument and work on that argument. If you return
a value, you can use that value when you call the function. Remember, after you define a function, you have to call the function in order to actually execute its contents.
Starter file
# Define a function `having_fun` that prints "Functions are FUN!".
# Define a function `thirty_seven` that prints the sum of 18 and 19.
# Call the two functions you've defined so far.
# Define a function `hello` that takes in a string parameter and prints the parameter variable.
# Call your `hello` function.
# Define a function `user_input` that asks the user "What is your name?" and stores it in a variable called `user_name` and print the user's name.
# Call your `user_input` function.
# Define a function `good_day` that creates an input dialogue asking the user "Are you having a nice day?" and prints the response.
# Call your `good_day` function.
# Define a function `average` that calculates the average between two parameters and returns the average.
# Call the `average` function and assign it to a variable `calculated_average`.
Instructions
Open the starter file and perform the following:
- Define a function
having_fun
that prints “Functions are FUN!” - Define a function
thirty_seven
that prints the sum of 18 and 19. - Call the two functions you’ve defined so far.
- Define a function
hello
that takes in a string parameter and prints the parameter variable. - Call your
hello
function. - Define a function
user_input
that asks the user “What is your name?”, stores it in a variable calleduser_name
, and prints the user’s name. - Call your
user_input
function. - Define a function
good_day
that creates an input dialogue asking the user “Are you having a nice day?” and prints the response. - Call your
good_day
function. - Define a function
average
that calculates the average between two parameters and returns the average. - Call the
average
function and assign it to a variablecalculated_average
. Then printcalculated_average
.
Solution
# Define a function `having_fun` that prints "Functions are FUN!".
def having_fun():
print("Functions are FUN!")
# Define a function `thirty_seven` that prints the sum of 18 and 19.
def thirty_seven():
sum = 18 + 19
print(sum)
# Call the two functions you've defined so far.
having_fun()
thirty_seven()
# Define a function `hello` that takes in a string parameter and prints the parameter variable.
def hello(string_param):
print(string_param)
# Call your `hello` function.
hello("Hello World!")
# Define a function `user_input` that asks the user "What is your name?" and stores it in a variable called `user_name` and print the user's name.
def user_input():
user_name = input("What is your name? ")
print(user_name)
# Call your `user_input` function.
user_input()
# Define a function `good_day` that creates an input dialogue asking the user "Are you having a nice day?" and prints the response.
def good_day():
response = input("Are you having a nice day? ")
print(response)
# Call your `good_day` function.
good_day()
# Define a function `average` that calculates the average between two parameters and returns the average.
def average(num_1, num_2):
avg_calc = num_1 + num_2 / 2
return avg_calc
# Call the `average` function and assign it to a variable `calculated_average`.
calculated_average = average(90, 10)
print(f"Calculated Average: {calculated_average}")