We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5c05335 commit b1d3d63Copy full SHA for b1d3d63
1 file changed
other/validate_age.py
@@ -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
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