Challenge - 5 Problems
Custom Module Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of importing and using a custom module function
Consider you have a file named
What will be the output of this code?
mymath.py with this content:def add(a, b):
return a + b
What will be the output of this code?
import mymath print(mymath.add(3, 4))
Python
import mymath print(mymath.add(3, 4))
Attempts:
2 left
💡 Hint
Remember that the function add returns the sum of two numbers.
✗ Incorrect
The function add in mymath module adds two numbers. Calling mymath.add(3, 4) returns 7, which is printed.
❓ Predict Output
intermediate2:00remaining
Output when importing a module with a syntax error
Given a file
What happens when you run this code?
mymodule.py with this content:def greet(name):
print(f"Hello, {name}!")
if True
print("This line has a syntax error")What happens when you run this code?
import mymodule
mymodule.greet("Alice")Python
import mymodule mymodule.greet("Alice")
Attempts:
2 left
💡 Hint
Check the if statement syntax in the module.
✗ Incorrect
The module has a missing colon after 'if True', causing a SyntaxError when imported.
🔧 Debug
advanced2:00remaining
Why does this custom module import fail?
You have a file
And this code in another file:
What is the cause of the error when running this code?
calculator.py with:def multiply(x, y):
return x * y
And this code in another file:
from Calculator import multiply print(multiply(2, 3))
What is the cause of the error when running this code?
Python
from Calculator import multiply print(multiply(2, 3))
Attempts:
2 left
💡 Hint
Python module names are case-sensitive and must match the file name exactly.
✗ Incorrect
The file is named 'calculator.py' but the import uses 'Calculator' with uppercase C. This causes ModuleNotFoundError.
📝 Syntax
advanced2:00remaining
Which import statement correctly imports all functions from a custom module?
You have a module
Which option correctly imports all functions so you can call them directly without prefix?
tools.py with several functions.Which option correctly imports all functions so you can call them directly without prefix?
Attempts:
2 left
💡 Hint
The syntax for importing all names from a module uses 'from module import *'.
✗ Incorrect
Option C uses correct Python syntax to import all functions from tools so they can be called directly.
🚀 Application
expert2:00remaining
What is the output of this custom module usage with __name__ check?
Given a file
And this code in another file:
What will be printed when running the second file?
greetings.py with:def say_hello():
print("Hello from greetings")
if __name__ == "__main__":
say_hello()And this code in another file:
import greetings
print("Imported greetings module")What will be printed when running the second file?
Python
import greetings print("Imported greetings module")
Attempts:
2 left
💡 Hint
Code inside 'if __name__ == "__main__"' runs only when the module is executed directly.
✗ Incorrect
When greetings.py is imported, __name__ is 'greetings', so the say_hello() inside the if block does not run. Only the print in the second file runs.