{"record":{"id":"64cc2daec0583adf","repo":"geekcomputers/Python","slug":"invalid-input-for-side","errorCode":null,"errorMessage":"Invalid input for side.","messagePattern":"Invalid input for side\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"area_of_square_app.py","lineNumber":42,"sourceCode":"# Example usage:\nnumber_str = \"two hundred fifteen\"\nresult = convert_words_to_number(number_str)\nprint(result)  # Output: 215\n\n\nclass Square:\n    def __init__(self, side=None):\n        if side is None:\n            self.ask_side()\n        # else:\n        #     self.side = float(side)\n        else:\n            if not isinstance(side, (int, float)):\n                try:\n                    side = float(side)\n                except ValueError:\n                    # return \"Invalid input for side.\"\n                    raise ValueError(\"Invalid input for side.\")\n            else:\n                self.side = float(side)\n        # Check if the result is a float and remove unnecessary zeros\n\n        self.calculate_square()\n        self.truncate_decimals()\n\n    # If ask side or input directly into the square.\n    # That can be done?\n    def calculate_square(self):\n        self.area = self.side * self.side\n        return self.area\n\n    # Want to add a while loop asking for the input.\n    # Also have an option to ask the user in true mode or in repeat mode.\n    def ask_side(self):\n        # if true bool then while if int or float then for loop.\n        # I will have to learn inheritance and polymorphism.","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/geekcomputers/Python/blob/40f4cd2652d75ef8e49d76e5c4d431d458712719/area_of_square_app.py#L24-L60","documentation":"This ValueError is raised by the Square class constructor when the side argument cannot be converted to a float. The code first accepts int/float directly; anything else (e.g., a string) is coerced via float(side), and if that raises ValueError — such as for 'abc' or '' — the code re-raises it as 'Invalid input for side.' The square's area is then computed and truncated, so an invalid side aborts object construction.","triggerScenarios":"Constructing Square('abc'), Square(''), Square(None) (raises TypeError, not this), or Square('12,5') with a comma decimal separator instead of a period. Any non-numeric string that float() cannot parse triggers this error.","commonSituations":"GUI or CLI input passed straight into the constructor without sanitization, locale differences where users type comma decimals, or trailing whitespace/units like '5 cm' (float would reject it).","solutions":["Pass a numeric value: Square(5) or Square(5.0)","Sanitize string input first: strip whitespace, replace ',' with '.', then try float() conversion","Validate input at the UI layer before constructing the object"],"exampleFix":"# before\nsquare = Square(input_value)  # input_value = 'abc'\n\n# after\ntry:\n    square = Square(float(input_value.replace(',', '.')))\nexcept ValueError:\n    print('Please enter a valid number')","handlingStrategy":"type-guard","validationCode":"try:\n    side_num = float(str(side).strip().replace(',', '.'))\nexcept ValueError:\n    side_num = None\nif side_num is not None:\n    square = Square(side_num)","typeGuard":"def is_valid_side(side) -> bool:\n    try:\n        v = float(side)\n        return v >= 0\n    except (TypeError, ValueError):\n        return False","tryCatchPattern":"try:\n    square = Square(side)\nexcept ValueError:\n    print('Please enter a valid number for the side length')","preventionTips":["Sanitize and strip string input before constructing the object","Convert comma decimal separators to periods for locale-tolerant parsing","Reject empty strings and strings with units at the UI layer"],"tags":["python","type-conversion","input-validation","valueerror"],"backgroundTag":"invalid-numeric-input","analyzedSha":"40f4cd2652d75ef8e49d76e5c4d431d458712719","analyzedAt":"2026-08-27T11:12:20.313Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}