Skip to content

Commit b1d3d63

Browse files
committed
Add validate_age script to others folder
1 parent 5c05335 commit b1d3d63

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

other/validate_age.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
This script prompts the user to input their age
3+
and validates that it is a non-negative integer.
4+
"""
5+
6+
def get_valid_age() -> int:
7+
"""
8+
Continuously prompts user for a valid age input until a non-negative integer is provided.
9+
10+
Returns:
11+
int: The validated age input.
12+
"""
13+
while True:
14+
age = input("Enter your age: ")
15+
if age.isdigit():
16+
num = int(age)
17+
if num < 0:
18+
print("Age can't be negative.")
19+
else:
20+
return num
21+
else:
22+
print("Invalid input. Please enter a valid number.")
23+
24+
25+
if __name__ == "__main__":
26+
age = get_valid_age()
27+
print(f"Your age is {age}")

0 commit comments

Comments
 (0)