程序运行仅输出"MONTH | AVERAGE",需实现各月步数统计展示
Hey Bryant, let's figure out why your step stats aren't showing up beyond the header! Here are the most common issues and fixes to get those monthly averages displaying properly:
1. 确认数据是否被正确加载
If your program isn't successfully pulling in step data (whether from a CSV, database, or even a hardcoded dataset), there's nothing to output beyond the header. Start here:
- Double-check your data source path (e.g., if reading a CSV, make sure
open('step_tracker_data.csv', 'r')points to the right file). - Print your loaded dataset to verify it's not empty:
print(monthly_step_data)— if it returns an empty list/dictionary, fix the data loading logic first.
2. Check if you're actually iterating over your data
You might have valid data, but forgot to loop through each month to calculate and display averages. For example, if your data is stored in a dictionary like {"January": [7500, 8200, ...], ...}, you need to loop through its items:
# Sample data structure monthly_steps = { "Jan": [8500, 9200, 7800], "Feb": [9100, 8700, 10200], "Mar": [7900, 8400, 9000] } # Print header first print("MONTH | AVERAGE") # Loop through each month to calculate and print averages for month, daily_steps in monthly_steps.items(): average = sum(daily_steps) / len(daily_steps) # Format output for readability print(f"{month: <5} | {average:.0f}")
Without this loop, you'll only ever see the header line.
3. Validate your average calculation logic
If your average calculation throws an error (like dividing by zero for a month with no data), your program might silently skip printing that month's data. Add a check to handle empty datasets:
for month, daily_steps in monthly_steps.items(): if not daily_steps: print(f"{month: <5} | No data") continue average = sum(daily_steps) / len(daily_steps) print(f"{month: <5} | {average:.0f}")
This ensures even months with missing data get a placeholder instead of being skipped entirely.
4. Verify your output statement placement
Make sure your monthly output line isn't trapped inside a conditional that never triggers (e.g., an if statement that always evaluates to False). Check indentation too — if your print line is incorrectly indented outside the loop, it won't run for each month.
Running the sample code above will produce:
MONTH | AVERAGE
Jan | 8500
Feb | 9333
Mar | 8433
Start with these checks, and you'll have those monthly stats showing up in no time!
内容的提问来源于stack exchange,提问作者Bryant Housemyer

