Skip to content

Commit 0ecb81b

Browse files
Enhance user input validation in name prompt to allow hyphens and spaces in names and embed minimum age variable into age validation for accuracy.
1 parent f8cec05 commit 0ecb81b

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

implement-laptop-allocation/laptop_allocaton.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import sys
23
from dataclasses import dataclass
34
from enum import Enum
@@ -63,15 +64,17 @@ def user_prompt() -> Person:
6364
"""
6465
try:
6566
# strip() whitespace before processing (no need for str type here as input always returns a string)
67+
# use 're' import (regular expression) for regex validation for allowed name characters
6668
name = input("Please enter your first name: ").strip()
67-
if not name.isalpha():
68-
raise ValueError("Name must contain only alphabetic characters.")
69+
if not re.fullmatch(r"[A-Za-z\- ]+", name):
70+
raise ValueError("Name must contain only alphabetic characters, hyphens, or spaces.")
71+
6972

7073
# strip() before converting to integer
7174
age = int(input("Please enter your age: ").strip())
7275
minimum_age = 18
7376
if age < minimum_age:
74-
raise ValueError("Age must be 18 or over.")
77+
raise ValueError("Age must be {minimum_age} or over.")
7578

7679

7780
# define valid OS options

0 commit comments

Comments
 (0)