diff --git a/electronics/ohms_law.py b/electronics/ohms_law.py index 66e737c1f909..490f774ae3da 100644 --- a/electronics/ohms_law.py +++ b/electronics/ohms_law.py @@ -4,23 +4,48 @@ def ohms_law(voltage: float, current: float, resistance: float) -> dict[str, float]: """ - Apply Ohm's Law, on any two given electrical values, which can be voltage, current, - and resistance, and then in a Python dict return name/value pair of the zero value. - - >>> ohms_law(voltage=10, resistance=5, current=0) - {'current': 2.0} - >>> ohms_law(voltage=0, current=0, resistance=10) - Traceback (most recent call last): - ... - ValueError: One and only one argument must be 0 - >>> ohms_law(voltage=0, current=1, resistance=-2) - Traceback (most recent call last): - ... - ValueError: Resistance cannot be negative - >>> ohms_law(resistance=0, voltage=-10, current=1) - {'resistance': -10.0} - >>> ohms_law(voltage=0, current=-1.5, resistance=2) - {'voltage': -3.0} + Apply Ohm's Law to calculate the missing electrical value. + + Ohm's Law: V = I × R, where V=voltage, I=current, R=resistance + + Args: + voltage: Electrical potential difference in volts (0 if unknown) + current: Electrical current in amperes (0 if unknown) + resistance: Electrical resistance in ohms (0 if unknown) + + Returns: + Dictionary with the calculated value: {'voltage': V}, {'current': I}, or {'resistance': R} + + Raises: + ValueError: If not exactly one value is 0, or if resistance is negative + + Examples: + >>> ohms_law(voltage=10, resistance=5, current=0) + {'current': 2.0} + >>> ohms_law(voltage=0, current=2, resistance=5) + {'voltage': 10.0} + >>> ohms_law(voltage=10, current=2, resistance=0) + {'resistance': 5.0} + >>> ohms_law(voltage=0, current=0, resistance=10) + Traceback (most recent call last): + ... + ValueError: One and only one argument must be 0 + >>> ohms_law(voltage=0, current=1, resistance=-2) + Traceback (most recent call last): + ... + ValueError: Resistance cannot be negative + >>> ohms_law(resistance=0, voltage=-10, current=1) + {'resistance': -10.0} + >>> ohms_law(voltage=0, current=-1.5, resistance=2) + {'voltage': -3.0} + >>> ohms_law(voltage=12, current=3, resistance=0) + {'resistance': 4.0} + >>> ohms_law(voltage=-9, current=3, resistance=0) + {'resistance': -3.0} + >>> ohms_law(voltage=0, current=2.5, resistance=4) + {'voltage': 10.0} + >>> ohms_law(voltage=7.5, current=0, resistance=3) + {'current': 2.5} """ if (voltage, current, resistance).count(0) != 1: raise ValueError("One and only one argument must be 0") diff --git a/web_programming/current_stock_price.py b/web_programming/current_stock_price.py index 531da949ea50..249c0364cc29 100644 --- a/web_programming/current_stock_price.py +++ b/web_programming/current_stock_price.py @@ -22,10 +22,27 @@ def stock_price(symbol: str = "AAPL") -> str: """ - >>> stock_price("EEEE") - 'No tag with the specified data-testid attribute found.' - >>> isinstance(float(stock_price("GOOG")),float) - True + Get current stock price from Yahoo Finance. + + Args: + symbol: Stock ticker symbol (e.g., 'AAPL', 'GOOG') + + Returns: + Current stock price as string, or error message if not found + + Examples: + >>> stock_price("EEEE") # Invalid symbol + 'No tag with the specified data-testid attribute found.' + >>> isinstance(float(stock_price("GOOG")), float) # Valid symbol + True + >>> stock_price("") # Empty symbol + 'No tag with the specified data-testid attribute found.' + >>> stock_price("INVALID_SYMBOL_123") # Another invalid symbol + 'No tag with the specified data-testid attribute found.' + >>> # Test output format for valid symbols + >>> result = stock_price("AAPL") + >>> result.replace(".", "").replace(",", "").isdigit() or "No " in result + True """ url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}" yahoo_finance_source = httpx.get(