Module 10: Practice Problems
15 problems to practice using modules and imports
Problem 1 Easy
Import the math module and use it to find the square root of 144. Print the result.
Use import math and then math.sqrt().
import math result = math.sqrt(144) print(result) # 12.0
Problem 2 Easy
Use math.ceil() and math.floor() to round 7.3 up and down. Print both results.
ceil() rounds up, floor() rounds down.
import math print(math.ceil(7.3)) # 8 print(math.floor(7.3)) # 7
Problem 3 Easy
Use math.pi to calculate the circumference of a circle with radius 10. Formula: circumference = 2 * pi * radius.
Access pi with math.pi (no parentheses - it is a constant, not a function).
import math radius = 10 circumference = 2 * math.pi * radius print(f"Circumference: {circumference:.2f}") # 62.83
Problem 4 Easy
Use random.randint() to simulate rolling two dice. Print each die and the total.
Use random.randint(1, 6) for each die.
import random die1 = random.randint(1, 6) die2 = random.randint(1, 6) print(f"Die 1: {die1}") print(f"Die 2: {die2}") print(f"Total: {die1 + die2}")
Problem 5 Easy
Use random.choice() to pick a random day of the week from a list.
Create a list of day names first, then use random.choice().
import random days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] print(f"Random day: {random.choice(days)}")
Problem 6 Medium
Use from...import to import only sqrt and pi from math. Calculate the area of a circle with radius 7.
Use from math import sqrt, pi then use them without the math. prefix.
from math import sqrt, pi radius = 7 area = pi * radius ** 2 print(f"Area: {area:.2f}") # 153.94
Problem 7 Medium
Get today's date using datetime and print it in the format "Month Day, Year" (e.g., "June 15, 2025").
Use strftime("%B %d, %Y") to format the date.
from datetime import datetime today = datetime.now() print(today.strftime("%B %d, %Y"))
Problem 8 Medium
Import the math module with the alias m. Use it to calculate the hypotenuse of a right triangle with sides 5 and 12.
Use import math as m and then m.sqrt(5**2 + 12**2).
import math as m hyp = m.sqrt(5**2 + 12**2) print(f"Hypotenuse: {hyp}") # 13.0
Problem 9 Medium
Create a password generator that picks 4 random words from a list to create a passphrase. Use random.sample().
random.sample(list, k) returns k unique random items from the list.
import random words = ["apple", "sunset", "river", "cloud", "tiger", "mountain", "ocean", "forest"] passphrase = random.sample(words, 4) print("-".join(passphrase))
Problem 10 Medium
Use random.shuffle() to shuffle a list of numbers 1-10, then print the shuffled list.
shuffle() modifies the list in place and returns None.
import random numbers = list(range(1, 11)) print("Before:", numbers) random.shuffle(numbers) print("After:", numbers)
Problem 11 Medium
Print the current time in 12-hour format with AM/PM (e.g., "02:30 PM").
Use strftime("%I:%M %p") where %I is 12-hour and %p is AM/PM.
from datetime import datetime now = datetime.now() print(now.strftime("%I:%M %p"))
Problem 12 Hard
Write a coin flip simulator that flips a coin 100 times and counts heads and tails. Print the results and the percentage of each.
Use random.choice(["Heads", "Tails"]) in a loop and count the results.
import random heads = 0 tails = 0 for _ in range(100): flip = random.choice(["Heads", "Tails"]) if flip == "Heads": heads += 1 else: tails += 1 print(f"Heads: {heads} ({heads}%)") print(f"Tails: {tails} ({tails}%)")
Problem 13 Hard
Write a function that takes a list of numbers and returns a dictionary with the floor, ceil, and square root of each number. Use the math module.
Create a dictionary for each number with keys "floor", "ceil", "sqrt" and use a loop or comprehension.
import math def analyze_numbers(numbers): results = {} for n in numbers: results[n] = { "floor": math.floor(n), "ceil": math.ceil(n), "sqrt": round(math.sqrt(n), 2) } return results nums = [7.3, 15.8, 25.0] for num, info in analyze_numbers(nums).items(): print(f"{num}: {info}")
Problem 14 Hard
Create a simple quiz game. Store 5 questions and answers in a dictionary. Use random.sample() to pick 3 random questions, ask the user (use input()), and keep score.
Use random.sample(list(quiz.items()), 3) to get 3 random question-answer pairs.
import random quiz = { "What is the capital of France?": "paris", "What color is the sky?": "blue", "How many days in a week?": "7", "What planet are we on?": "earth", "What is 5 + 3?": "8" } questions = random.sample(list(quiz.items()), 3) score = 0 for question, answer in questions: user_answer = input(question + " ") if user_answer.lower().strip() == answer: print("Correct!") score += 1 else: print(f"Wrong! The answer is {answer}") print(f"\nScore: {score}/3")
Problem 15 Hard
Write a module-style file with three functions: greet(name), farewell(name), and current_time(). Include an if __name__ == "__main__" block that tests all three functions.
Write the functions first, then add the if __name__ == "__main__": guard with test calls.
# greetings.py from datetime import datetime def greet(name): return f"Hello, {name}! Nice to meet you." def farewell(name): return f"Goodbye, {name}! See you next time." def current_time(): now = datetime.now() return now.strftime("%I:%M %p") if __name__ == "__main__": print(greet("Alice")) print(farewell("Alice")) print(f"Current time: {current_time()}")