geekcomputers/Python · error · ValueError
Invalid input for side.
Error message
Invalid input for side.
What it means
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.
Source
Thrown at area_of_square_app.py:42
# Example usage:
number_str = "two hundred fifteen"
result = convert_words_to_number(number_str)
print(result) # Output: 215
class Square:
def __init__(self, side=None):
if side is None:
self.ask_side()
# else:
# self.side = float(side)
else:
if not isinstance(side, (int, float)):
try:
side = float(side)
except ValueError:
# return "Invalid input for side."
raise ValueError("Invalid input for side.")
else:
self.side = float(side)
# Check if the result is a float and remove unnecessary zeros
self.calculate_square()
self.truncate_decimals()
# If ask side or input directly into the square.
# That can be done?
def calculate_square(self):
self.area = self.side * self.side
return self.area
# Want to add a while loop asking for the input.
# Also have an option to ask the user in true mode or in repeat mode.
def ask_side(self):
# if true bool then while if int or float then for loop.
# I will have to learn inheritance and polymorphism.View on GitHub (pinned to 40f4cd2652)
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
Example fix
# before
square = Square(input_value) # input_value = 'abc'
# after
try:
square = Square(float(input_value.replace(',', '.')))
except ValueError:
print('Please enter a valid number') Defensive patterns
Strategy: type-guard
Validate before calling
try:
side_num = float(str(side).strip().replace(',', '.'))
except ValueError:
side_num = None
if side_num is not None:
square = Square(side_num) Type guard
def is_valid_side(side) -> bool:
try:
v = float(side)
return v >= 0
except (TypeError, ValueError):
return False Try / catch
try:
square = Square(side)
except ValueError:
print('Please enter a valid number for the side length') Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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).
Related errors
- Parameter n must be int or passive of cast to int.
- Parameter n must be greater or equal to one.
- Format must be HH:MM:SS
- Time out of range
- Degrees must be between 0 and 359
AI-assisted analysis of geekcomputers/Python@40f4cd2652 (2026-08-27).
Data as JSON: /api/errors/64cc2daec0583adf.
Report an issue: GitHub.