{"record":{"id":"b9f589f6effe06e6","repo":"TheAlgorithms/Python","slug":"token-is-neither-a-number-nor-a-valid-operator","errorCode":null,"errorMessage":"{token} is neither a number nor a valid operator","messagePattern":"(.+?) is neither a number nor a valid operator","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"data_structures/stacks/postfix_evaluation.py","lineNumber":61,"sourceCode":"    returns the data as it is with a False flag. This function also serves as a check\n    of whether the input is a number or not.\n\n    Parameters\n    ----------\n    token: The data that needs to be converted to the appropriate operator or number.\n\n    Returns\n    -------\n    float or str\n        Returns a float if `token` is a number or a str if `token` is an operator\n    \"\"\"\n    if token in OPERATORS:\n        return token\n    try:\n        return float(token)\n    except ValueError:\n        msg = f\"{token} is neither a number nor a valid operator\"\n        raise ValueError(msg)\n\n\ndef evaluate(post_fix: list[str], verbose: bool = False) -> float:\n    \"\"\"\n    Evaluate postfix expression using a stack.\n    >>> evaluate([\"0\"])\n    0.0\n    >>> evaluate([\"-0\"])\n    -0.0\n    >>> evaluate([\"1\"])\n    1.0\n    >>> evaluate([\"-1\"])\n    -1.0\n    >>> evaluate([\"-1.1\"])\n    -1.1\n    >>> evaluate([\"2\", \"1\", \"+\", \"3\", \"*\"])\n    9.0\n    >>> evaluate([\"2\", \"1.9\", \"+\", \"3\", \"*\"])","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/stacks/postfix_evaluation.py#L43-L79","documentation":"Raised by convert_token() in data_structures/stacks/postfix_evaluation.py:61 when a token is neither in the OPERATORS dict ('+','-','*','/','^','%') nor parseable by float(). It is the input-validation stage of evaluate(): each space-separated token must already be a number or a known operator symbol.","triggerScenarios":"evaluate(['2','&','3']), passing '**' instead of '^', comma-formatted numbers like '1,000', or an empty token from double spaces ('1  +  2'.split(' ') yields '').","commonSituations":"Splitting on a single space instead of str.split() (which collapses whitespace), locale-specific number formats, or using operators the evaluator never defined.","solutions":["Tokenize with expression.split() (no argument) so empty tokens never occur","Normalize operators before evaluation (e.g. '**' -> '^')","Catch ValueError and report the offending token back to the user"],"exampleFix":"// before\ntokens = expression.split(' ')  # '1  +  2' -> ['', '+', '', '+', '2']\n\n# after\ntokens = expression.split()  # collapses all whitespace, no empty tokens","handlingStrategy":"validation","validationCode":"tokens = expression.split()  # no arg: collapses all whitespace, no '' tokens\ntokens = [t.replace('**', '^') for t in tokens]","typeGuard":"from data_structures.stacks.postfix_evaluation import OPERATORS\n\ndef all_tokens_valid(tokens: list[str]) -> bool:\n    return all(t in OPERATORS or _is_float(t) for t in tokens)\n\ndef _is_float(t: str) -> bool:\n    try:\n        float(t)\n        return True\n    except ValueError:\n        return False","tryCatchPattern":"try:\n    result = evaluate(tokens)\nexcept ValueError as e:\n    if 'neither a number nor a valid operator' not in str(e):\n        raise\n    # report offending token from the message back to the user","preventionTips":["Never split with split(' ') — double spaces create empty tokens","Normalize '**' to '^' and reject locale-formatted numbers before evaluating"],"tags":["stack","expression-evaluation","validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}