Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Why Use Functions?
What is the main benefit of using functions in your code?
Hint : Think about the DRY principle: Don't Repeat Yourself!
Exercise 2 — Complete the Function Structure
Complete this function that takes a Codémon name as parameter and returns a greeting:
Hint : Remember: def to define, : after parameters, return to send back a value, and quotes for string arguments.
Exercise 3 — Return vs Print - Understanding the Difference
What's the difference between these two functions?
def function_a():
print(100)
def function_b():
return 100function_a displays 100 but returns None, function_b returns 100 without displayingfunction_a returns 100, function_b displays 100Hint : print() displays to screen but doesn't return a value. return sends a value back but doesn't display.
Exercise 4 — Function Terminology Review
Match each term with its correct meaning:
Exercise 5 — Local Variable Access
What happens when you try to access a local variable outside its function?
def battle():
damage = 50
print(damage)
battle()
print(damage)NameError occurs because damage doesn't exist outside the functionHint : Local variables only exist inside their function and are destroyed when the function ends.
Exercise 6 — Function with Default Parameter
Complete this function where element has a default value of "water":
Hint : Use = to set default values in function definition. String arguments need quotes.
Exercise 7 — Parameter Order Rules
Which function definition is correct?
Hint : Parameters with default values must come AFTER parameters without defaults.
Exercise 8 — Using Return Values in Calculations
Complete the code to calculate total stats by adding the return values of both functions:
Hint : Use return to send values back, then call the functions with () to get their return values.
Exercise 9 — What Does This Function Return?
What value does this function return?
def show_level(level):
print(f"Level: {level}")
result = show_level(25)None25"Level: 25"levelHint : Functions without a return statement automatically return None.
Exercise 10 — Scope Concepts
Match each scope concept with its example:
global keywordExercise 11 — Return Value Data Types
What can a function return?
Hint : Functions are very flexible with what they can return!
Exercise 12 — Function with Three Parameters
Complete this function that takes 3 parameters and returns their sum:
Hint : Use + to add values, return to send back result, and commas to separate multiple arguments.
Exercise 13 — Variable Shadowing
What does this code print?
level = 10
def show_level():
level = 25
print(level)
show_level()2510NameError10 then 25Hint : The local level inside the function shadows (hides) the global one.
Exercise 14 — Function Returning Boolean
Complete this function that returns True if HP is above 50, otherwise False:
Hint : Use > to compare, and return to send back the boolean result.
Exercise 15 — When Are Parameters Needed?
Given this function definition:
def heal_codemon(name, hp):
return f"{name} healed for {hp} HP!"Which function call is correct?
heal_codemon("Aquachu", 30)heal_codemon()heal_codemon("Aquachu")heal_codemon(name="Aquachu")Hint : When calling a function, you must provide values for all parameters that don't have default values.
Exercise 16 — Function Behaviors
Match each function characteristic with its behavior:
returnreturnprintNone automaticallyNoneExercise 17 — Function Returning a List
Complete this function that returns a list of Codémon moves:
Hint : Lists start with [, functions return values with return, and string arguments need quotes.
Exercise 18 — Function Naming Best Practice
Which function name follows Python best practices?
calculate_damageCalculateDamagecalculateDamageCALCULATE_DAMAGEHint : Python functions use lowercase with underscores (snake_case).
Exercise 19 — Conditional Returns
Complete this function with conditional return values:
Hint : Use <= for "less than or equal", and to combine conditions, and return to send back values.
Exercise 20 — Default Parameter Values
What happens if you don't provide an argument for a parameter with a default value?
NoneTypeErrorHint : Default values exist specifically to make parameters optional!
Exercise 21 — Function Returning a Dictionary
Complete this function that returns Codémon stats as a dictionary:
Hint : Dictionaries start with {, use return to send them back, separate arguments with commas, and access values with ["key"].
Exercise 22 — Parameter vs Argument
In this code, what is "Pyrochu"?
def display_codemon(name):
print(name)
display_codemon("Pyrochu")Hint : Parameter is in the definition, argument is the actual value passed.
Exercise 23 — Calling Multiple Functions
Complete the code where one function uses the result of another:
Hint : Both functions need return to send values back. Call the first function with () and store its result.
Exercise 24 — When to Use global Keyword
When do you need the global keyword inside a function?
Hint : You can read global variables freely, but need global to change their values.
Exercise 25 — Function Concepts Review
Match each concept with its correct definition:
return statementsnake_case=value, becomes optional