Your First Program: A Golf Score Calculator

Now that your environment is set up, let’s write some real code. We’ll build a simple golf score calculator, step by step.

What You’ll Learn

  • Variables and basic data types (strings, integers, floats)
  • print() and f-strings for output
  • Basic arithmetic
  • Getting input from the user
  • Writing and running a Python script

Concept: How Programs Work

At its simplest, a program:

  1. Takes input — data from the user, a file, a database, or an API
  2. Processes it — applies logic, calculations, or transformations
  3. Produces output — displays results, writes files, or sends data somewhere

Our golf score calculator will: - Input: Scores for each hole - Process: Calculate total score, compare to par, compute basic stats - Output: Display a round summary


Code: Building the Score Calculator

Step 1: Variables and Types

Variables store data. Python figures out the type automatically.

# Strings — text
player_name = "Bear Woods"
course_name = "North Park Golf Course"

# Integers — whole numbers
par = 72
score = 78

# Floats — decimal numbers
handicap = 13.9

# Booleans — True or False
made_cut = True

print(f"Player: {player_name}")
print(f"Course: {course_name}")
print(f"Score: {score} (par {par})")
print(f"Handicap: {handicap}")
print(f"Made cut: {made_cut}")
# You can check the type of any variable
print(type(player_name))  # <class 'str'>
print(type(par))          # <class 'int'>
print(type(handicap))     # <class 'float'>
print(type(made_cut))     # <class 'bool'>

Step 2: Arithmetic and F-Strings

Python supports all the math you’d expect. F-strings (the f"..." syntax) let you embed expressions directly in strings.

# A 9-hole front nine
hole_scores = [4, 3, 5, 5, 4, 3, 6, 4, 4]
hole_pars   = [4, 3, 5, 4, 4, 3, 5, 4, 4]

# Calculate totals
total_score = sum(hole_scores)
total_par = sum(hole_pars)
relative = total_score - total_par

# Format the relative score like golfers do (+2, -1, E for even)
if relative > 0:
    relative_display = f"+{relative}"
elif relative < 0:
    relative_display = str(relative)
else:
    relative_display = "E"

print(f"Front 9: {total_score} ({relative_display})")

Step 3: Lists — Storing Multiple Values

Lists are ordered collections. Perfect for storing scores across 18 holes.

# Full 18-hole round at North Park
pars   = [4, 3, 5, 4, 4, 3, 5, 4, 4, 4, 3, 5, 4, 4, 3, 5, 4, 4]
scores = [4, 3, 5, 5, 4, 3, 6, 4, 4, 5, 3, 5, 5, 4, 3, 5, 4, 5]

print(f"Number of holes: {len(scores)}")
print(f"First hole score: {scores[0]}")
print(f"Last hole score: {scores[-1]}")
print(f"Front 9: {sum(scores[:9])}")
print(f"Back 9: {sum(scores[9:])}")
print(f"Total: {sum(scores)}")

Step 4: Putting It All Together

Let’s build a function that prints a complete round summary.

def format_relative_score(score: int, par: int) -> str:    """Format a score relative to par (e.g., +3, -1, E)."""    diff = score - par    if diff > 0:        return f"+{diff}"    elif diff < 0:        return str(diff)    return "E"def print_round_summary(player: str, course: str, pars: list, scores: list) -> None:    """Print a formatted round summary."""    total_par = sum(pars)    total_score = sum(scores)    front_9 = sum(scores[:9])    back_9 = sum(scores[9:])    # Count scoring types    albatrosses = sum(1 for s, p in zip(scores, pars) if s <= p - 3)    eagles = sum(1 for s, p in zip(scores, pars) if s == p - 2)    birdies = sum(1 for s, p in zip(scores, pars) if s == p - 1)    pars_made = sum(1 for s, p in zip(scores, pars) if s == p)    bogeys = sum(1 for s, p in zip(scores, pars) if s == p + 1)    doubles_or_worse = sum(1 for s, p in zip(scores, pars) if s >= p + 2)    print(f"{'=' * 40}")    print(f"  {player}")    print(f"  {course}")    print(f"{'=' * 40}")    print(f"  Front 9:  {front_9}")    print(f"  Back 9:   {back_9}")    print(f"  Total:    {total_score} ({format_relative_score(total_score, total_par)})")    print(f"{'─' * 40}")    if albatrosses:        print(f"  Albatrosses:      {albatrosses}")    if eagles:        print(f"  Eagles:           {eagles}")    print(f"  Birdies:          {birdies}")    print(f"  Pars:             {pars_made}")    print(f"  Bogeys:           {bogeys}")    print(f"  Double or worse:  {doubles_or_worse}")    print(f"{'=' * 40}")
# Try it out!
pars   = [4, 3, 5, 4, 4, 3, 5, 4, 4, 4, 3, 5, 4, 4, 3, 5, 4, 4]
scores = [4, 3, 5, 5, 4, 3, 6, 4, 4, 5, 3, 5, 5, 4, 3, 5, 4, 5]

print_round_summary("Bear Woods", "North Park Golf Course", pars, scores)

Step 5: Save It as a Script

Everything above works in a notebook, but you can also save it as a standalone .py file and run it from the terminal.

Create a file called score_calculator.py with the functions above, add this at the bottom:

if __name__ == "__main__":
    pars   = [4, 3, 5, 4, 4, 3, 5, 4, 4, 4, 3, 5, 4, 4, 3, 5, 4, 4]
    scores = [4, 3, 5, 5, 4, 3, 6, 4, 4, 5, 3, 5, 5, 4, 3, 5, 4, 5]
    print_round_summary("Bear Woods", "North Park Golf Course", pars, scores)

Then run it:

python3 score_calculator.py

The if __name__ == "__main__": block only runs when the file is executed directly — not when it’s imported as a module. You’ll learn more about this in the next notebook.


AI: Let AI Write It, Then Evaluate

Now that you’ve built the score calculator by hand, let’s see what AI does with the same task.

Exercise: AI-Generated Score Calculator

Open Claude or ChatGPT and try this prompt:

“Write a Python function that takes a list of hole pars and a list of hole scores for an 18-hole round of golf. It should print a summary showing: front 9, back 9, total score, score relative to par, and a count of birdies, pars, bogeys, and doubles or worse.”

Questions to Ask Yourself

  1. Does it work? Copy the AI’s code into a cell below and run it. Does it produce the correct output?
  2. Is it different from yours? How did the AI’s approach differ? Did it use any Python features you haven’t seen yet?
  3. Is it better or worse? Is the AI’s code more readable? More concise? Did it handle edge cases you missed (like an eagle or a hole-in-one)?
  4. What if the input is bad? What happens if you pass in 17 scores instead of 18? Does the AI’s version handle that?

Try It Below

# Paste the AI-generated code here and run it
# Test the AI's version with the same data
pars   = [4, 3, 5, 4, 4, 3, 5, 4, 4, 4, 3, 5, 4, 4, 3, 5, 4, 4]
scores = [4, 3, 5, 5, 4, 3, 6, 4, 4, 5, 3, 5, 5, 4, 3, 5, 4, 5]

# Call the AI's function here

Prompt Engineering Tip

Notice how the quality of the AI’s output depends on how specific your prompt is. Try making the prompt more specific:

“Write a Python function called print_round_summary that takes four parameters: player name (str), course name (str), pars (list of ints), and scores (list of ints). Use type hints. Format the output as a clean table. Include eagles in the scoring breakdown. Use f-strings.”

The more context you give, the better the output. This is a skill you’ll develop throughout the course.


Summary

You’ve written your first Python program! Along the way you learned:

  • Variables store data (strings, ints, floats, booleans)
  • Lists hold multiple values (scores = [4, 3, 5, ...])
  • F-strings embed expressions in strings (f"Score: {total}")
  • Functions wrap reusable logic (def print_round_summary(...))
  • AI can write code, but you need to evaluate whether it’s correct

Next up: Python modules and packages — how to organize code and use libraries.

Get the Complete Course Bundle

All notebooks, the full golf dataset, and new tutorials — straight to your inbox.