Educational collection of surprising Python code snippets that demonstrate counter-intuitive behaviors and language internals
Overall
score
93%
Build a simple financial calculator module that handles monetary calculations with proper rounding behavior and validates numeric inputs to prevent precision errors.
Your calculator should handle three key operations:
When rounding amounts to 2 decimal places:
Your converter should:
Your validator should check if a float value equals its expected mathematical value:
@generates
def round_amount(value: float, decimals: int = 2) -> float:
"""
Round a monetary amount using banker's rounding (round half to even).
Args:
value: The amount to round
decimals: Number of decimal places (default: 2)
Returns:
Rounded value
"""
pass
def convert_string_to_number(value_str: str) -> float:
"""
Convert a string to a number with safety checks.
Args:
value_str: String representation of a number
Returns:
The numeric value
Raises:
ValueError: If the string is too long (over 4000 digits)
"""
pass
def is_precise(value: float) -> bool:
"""
Check if a float value is precisely representable.
Args:
value: The float value to check
Returns:
True if the value is precise, False if there are precision errors
"""
passProvides educational insights into Python numeric behaviors, including banker's rounding, integer string conversion limits, and float precision quirks.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/pypi-wtfpythondocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10