Assertions
Completed
Write average(nums) that starts with assert nums, "empty list". Read a line (possibly just - meaning empty) and print the average with 2 decimals or AssertionError: empty list.
Input
One line of integers, or -.
Output
One line.
Example
Input
1 2 3 4
Output
2.50
Editor
Python
Hints
assert condition, message raises AssertionError when the condition is false.
Solution explanation
Assertions check assumptions during development. Don't use them to validate user input in production - they can be disabled with python -O.
Show solution
def average(nums):
assert nums, "empty list"
return sum(nums) / len(nums)
text = input()
nums = [] if text == "-" else list(map(int, text.split()))
try:
print(f"{average(nums):.2f}")
except AssertionError as err:
print(f"AssertionError: {err}")