0
0
Pythonprogramming~20 mins

Why functions are needed in Python - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of code using a function to avoid repetition
What is the output of this code that uses a function to print a greeting twice?
Python
def greet(name):
    print(f"Hello, {name}!")

greet("Alice")
greet("Alice")
AHello, Alice!\nHello, Alice!
BHello, Alice!\nHello, Bob!
CHello, Alice!\nHello, Alice!\nHello, Alice!
DHello, Alice!
Attempts:
2 left
๐Ÿ’ก Hint
Look at how many times the greet function is called.
๐Ÿง  Conceptual
intermediate
1:30remaining
Why use functions to organize code?
Why do programmers use functions in their code?
ATo confuse other programmers.
BTo repeat the same code many times manually.
CTo make the program run slower.
DTo organize code into reusable blocks and avoid repetition.
Attempts:
2 left
๐Ÿ’ก Hint
Think about how functions help with repeating tasks.
โ“ Predict Output
advanced
1:30remaining
Output of code without functions causing repetition
What is the output of this code that repeats similar print statements without using a function?
Python
print("Hello, Bob!")
print("Hello, Bob!")
print("Hello, Bob!")
AHello, Bob!\nHello, Alice!\nHello, Bob!
BHello, Bob!\nHello, Bob!\nHello, Bob!
CHello, Bob!
DHello, Bob!\nHello, Bob!
Attempts:
2 left
๐Ÿ’ก Hint
Count how many print statements there are.
๐Ÿง  Conceptual
advanced
1:30remaining
Benefit of functions for changing code easily
How do functions help when you want to change a repeated task in your program?
AYou can change the code in one place inside the function.
BYou must change every repeated line manually.
CFunctions make it impossible to change code.
DYou have to rewrite the whole program.
Attempts:
2 left
๐Ÿ’ก Hint
Think about how many places you need to edit if you use a function.
โ“ Predict Output
expert
2:30remaining
Output of code demonstrating function reuse with different inputs
What is the output of this code that uses a function to greet different people?
Python
def greet(name):
    return f"Hello, {name}!"

messages = [greet("Anna"), greet("Ben"), greet("Cara")]
for message in messages:
    print(message)
AHello, Anna!\nHello, Anna!\nHello, Anna!
BHello, Ben!\nHello, Cara!\nHello, Anna!
CHello, Anna!\nHello, Ben!\nHello, Cara!
DHello, Cara!\nHello, Ben!\nHello, Anna!
Attempts:
2 left
๐Ÿ’ก Hint
Look at the order of names in the list.