Knowing syntax and being able to build something are different skills. This lesson walks through structuring a small, complete command-line program from scratch.
Planning before coding
Before writing a line of code, write down: what does the program do, what data does it need to remember, and what are the menu options a user sees. For a simple to-do list app:
- Add a task
- View all tasks
- Mark a task as done
- Exit
Building the menu loop
def main():
tasks = []
while True:
print("\n1. Add task")
print("2. View tasks")
print("3. Exit")
choice = input("Choose an option: ")
if choice == "1":
task = input("Task description: ")
tasks.append({"task": task, "done": False})
elif choice == "2":
for i, t in enumerate(tasks):
status = "x" if t["done"] else " "
print(f"[{status}] {i + 1}. {t['task']}")
elif choice == "3":
break
else:
print("Invalid option, try again.")
if __name__ == "__main__":
main()
Why if __name__ == "__main__":
This guard means main() only runs when you execute this file directly - not if you later import it as a module into another file. It is a standard convention in almost every real Python script.
Extending it yourself
Once this runs, try adding: marking a task done (loop through tasks and flip "done" to True), deleting a task, or saving tasks to a file using the pattern from the File I/O lesson so they persist between runs. This kind of self-directed extension is exactly what turns a tutorial exercise into a real portfolio project.