The bug is division by zero when an empty list is passed. len(nums) is 0, causing total / len(nums) to raise a ZeroDivisionError.
Here's the fixed version:
def average(nums):
if not nums:
return 0 # or raise ValueError("Cannot compute average of empty list")
total = 0
for n in nums:
total += n
return total / len(nums)
print(average([]))
The fix checks if the list is empty before computing the average. Returning 0 is one option; alternatively, you could raise an exception or return None depending on what makes sense for your use case.