Bắt đầu ngayBắt đầu miễn phí

Debug Python Code with Claude Code

Your colleague reports a strange bug: the calculate_average_tracks() function in analytics.py is returning incorrect values. When testing with 3 albums having 10, 12, and 8 tracks respectively, the function returns 30 instead of the expected average of 10.

Here's the function in analytics.py:

def calculate_average_tracks(albums):
    """Calculate average number of tracks per album.

    Returns the average track count across all albums.
    """
    total = sum(album.get('track_count', 0) for album in albums)
    return total  # Bug: should return total / len(albums)

    # Bug reproduction:
    # albums = [{'track_count': 10}, {'track_count': 12}, {'track_count': 8}]
    # print(calculate_average_tracks(albums))  # Returns 30, should return 10

Bài tập này là một phần của khóa học

Software Development with Claude Code

Xem khóa học

Hướng dẫn bài tập

  • Start Claude Code with claude.
  • Ask Claude to analyze analytics.py and identify bugs in calculate_average_tracks().
  • Ask Claude to fix the bug.
  • When done, type /exit to exit Claude Code.

Bài tập tương tác thực hành

Biến lý thuyết thành hành động với một trong các bài tập tương tác của chúng tôi

Bắt đầu bài tập