Preplp
Daily Python
Day 6Python~35 min

Functions

Define reusable blocks and return values

Track progress0/15 days
x

What you'll learn

defparametersreturnrandom
  • Write functions with and without return values
  • Pass arguments into custom functions
  • Build a small math quiz game with functions

Lesson & examples

Read the code, then the explanation — try changing values before the project.

Basic function
def greet_user(name):
    print(f"Hello, {name}! Welcome to Python!")

greet_user("Alex")
Return a value
def multiply(a, b):
    return a * b

result = multiply(5, 4)
print("The result is:", result)

Today's exercise: Math Quiz Game

Generate random math questions and score the player over five rounds.

Steps

  1. 1

    generate_question()

    Pick two numbers and an operator (+, -, *). Return the question string and correct answer.

  2. 2

    math_quiz()

    Loop five times, ask the user, update score, print final result.

Full project source — copy into a folder or use the zip above

starter.py
import random

def generate_question():
    num1 = random.randint(1, 10)
    num2 = random.randint(1, 10)
    operator = random.choice(["+", "-", "*"])
    if operator == "+":
        answer = num1 + num2
    elif operator == "-":
        answer = num1 - num2
    else:
        answer = num1 * num2
    return f"{num1} {operator} {num2}", answer

def math_quiz():
    score = 0
    print("\n--- Welcome to the Math Quiz Game! ---")
    for i in range(5):
        question, correct = generate_question()
        print(f"\nQuestion {i + 1}: {question}")
        user_answer = int(input("Your answer: "))
        if user_answer == correct:
            print("Correct!")
            score += 1
        else:
            print(f"Wrong! The correct answer is: {correct}")
    print(f"\nYour final score: {score}/5")

math_quiz()

One rehearsal platform

Certification mocks, daily lessons, project labs, and in-browser drills

Structured for exam day and portfolio proof — timed tests, guided builds, and quick reps on one platform.