15. Putting It Together

Putting It Together

You have learned variables, operators, strings, conditionals, loops, data structures, functions, modules, files, error handling, and classes. That is everything you need to build real programs. In this final chapter you will combine these ideas into three small projects, then plan how to keep growing as a programmer.

Project 1: Number-guessing game

The computer picks a secret number and you guess until you find it. This project uses the random module, a while loop, conditionals, and input conversion with error handling:

PYTHON
import random

secret = random.randint(1, 20)
guesses = 0

print("I'm thinking of a number from 1 to 20.")
while True:
    try:
        guess = int(input("Your guess: "))
    except ValueError:
        print("Please type a whole number.")
        continue
    guesses += 1
    if guess < secret:
        print("Too low.")
    elif guess > secret:
        print("Too high.")
    else:
        print(f"Correct! You took {guesses} guesses.")
        break
▸ OUTPUT (example session)
I'm thinking of a number from 1 to 20.
Your guess: 10
Too high.
Your guess: 5
Too low.
Your guess: 7
Correct! You took 3 guesses.

The while True loop runs forever until break stops it on a correct guess. The try/except keeps a bad entry from crashing the game.

Project 2: Temperature converter

This project turns the conversion formulas into reusable functions, then offers the user a simple menu. It practises functions, f-strings, and clean output formatting:

PYTHON
def c_to_f(c):
    return c * 9 / 5 + 32

def f_to_c(f):
    return (f - 32) * 5 / 9

choice = input("Convert C to F or F to C? ").strip().upper()
value = float(input("Enter the temperature: "))

if choice == "C TO F":
    print(f"{value}C = {c_to_f(value):.1f}F")
elif choice == "F TO C":
    print(f"{value}F = {f_to_c(value):.1f}C")
else:
    print("Unknown choice.")
▸ OUTPUT (user chose C to F and typed 100)
Convert C to F or F to C? c to f
Enter the temperature: 100
100.0C = 212.0F

Because we called .upper() on the choice, the user can type in any mix of upper and lower case. Each formula lives in its own function, so you could reuse them in another program.

Project 3: Simple to-do list

This project stores tasks in a list and lets the user add, view, and remove them in a loop. It brings together lists, a menu-driven while loop, and functions:

PYTHON
tasks = []

def show(tasks):
    if not tasks:
        print("(no tasks)")
    for i, task in enumerate(tasks, start=1):
        print(f"{i}. {task}")

while True:
    command = input("add / list / quit: ").strip().lower()
    if command == "add":
        tasks.append(input("Task: "))
    elif command == "list":
        show(tasks)
    elif command == "quit":
        print("Goodbye!")
        break
    else:
        print("Unknown command.")
▸ OUTPUT (example session)
add / list / quit: add
Task: Buy milk
add / list / quit: add
Task: Call Sam
add / list / quit: list
1. Buy milk
2. Call Sam
add / list / quit: quit
Goodbye!

The built-in enumerate function gives both a counter and each item, which is perfect for numbered lists. As an extension, you could save the tasks to a file so they survive after the program closes, using the file-handling skills from an earlier chapter.

How to keep learning

You now have a solid foundation. To grow further:

  • Build things. Pick small projects you find useful: a tip calculator, a password generator, a quiz. Building cements knowledge far better than reading alone.
  • Read the official docs. The Python documentation and its tutorial at docs.python.org are excellent and free.
  • Learn to debug. Read error messages carefully, print values to see what your program is really doing, and change one thing at a time.
  • Explore libraries. When ready, try pandas for data, flask for web apps, or pygame for games.
  • Practice regularly. A little coding most days beats an occasional marathon. Sites with small exercises are great for keeping sharp.

Congratulations on reaching the end of this course. Programming is a craft you improve for a lifetime, and you have taken the most important step: starting. Keep writing code, stay curious, and enjoy the power to turn your ideas into working programs.