question 179 Exceptions & Validation · hard
Validate a sign-up form, every field at once
A sign-up form must show every problem at once, next to the field it belongs to. Write one checker per field that raises ValueError with the field's message, then validate_signup(form) that runs them all and collects the messages.
Function
validate_signup(form) returns {field: message} for every invalid field, in the order username, email, age; {} when all are validusername3–20 characters, only letters, digits and
_: message username must be 3-20 letters, digits or _emailexactly one
@, with at least one . after it and something before it: message email address is not validagea whole number from 13 to 120 (text from the form): message
age must be a whole number, or age must be 13-120Missinga field missing from
form counts as the empty text ""Examples
callvalidate_signup({'username': 'ana_dev', 'email': 'ana@runtime.school', 'age': '34'})returns{}
callvalidate_signup({'username': 'a!', 'email': 'ana.runtime.school', 'age': 'twelve'})returns{'username': 'username must be 3-20 letters, digits or _', 'email': 'email address is not valid', 'age': 'age must be a whole number'}
callvalidate_signup({'username': 'theo99', 'email': 'theo@mail.com', 'age': '130'})returns{'age': 'age must be 13-120'}