Skip to content

Commit 9407d88

Browse files
committed
Solved the issue named Python from the github repo which includes the python code as i have contributed.
1 parent 709c18e commit 9407d88

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Age_difference/main.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
try:
2+
# Get boy's name
3+
boyName = input("Boy Name : ")
4+
if not boyName or not boyName.strip():
5+
raise ValueError("Boy name cannot be empty")
6+
if not boyName.replace(" ", "").isalpha():
7+
raise TypeError("Boy name must contain only alphabetic characters")
8+
9+
# Get boy's age
10+
boyAgeInput = input("Boy age : ")
11+
if not boyAgeInput.strip():
12+
raise ValueError("Boy age cannot be empty")
13+
try:
14+
boyAge = int(boyAgeInput)
15+
if boyAge <= 0:
16+
raise ValueError("Boy age must be a positive number")
17+
except ValueError as e:
18+
if "invalid literal" in str(e):
19+
raise TypeError("Boy age must be a valid integer")
20+
raise
21+
22+
# Get girl's name
23+
girlName = input("Girl Name : ")
24+
if not girlName or not girlName.strip():
25+
raise ValueError("Girl name cannot be empty")
26+
if not girlName.replace(" ", "").isalpha():
27+
raise TypeError("Girl name must contain only alphabetic characters")
28+
29+
# Get girl's age
30+
girlAgeInput = input("Girl age : ")
31+
if not girlAgeInput.strip():
32+
raise ValueError("Girl age cannot be empty")
33+
try:
34+
girlAge = int(girlAgeInput)
35+
if girlAge <= 0:
36+
raise ValueError("Girl age must be a positive number")
37+
except ValueError as e:
38+
if "invalid literal" in str(e):
39+
raise TypeError("Girl age must be a valid integer")
40+
raise
41+
42+
# Calculate and display result
43+
print(boyName, "loves", girlName, ". Age difference is", abs(boyAge - girlAge))
44+
45+
except TypeError as e:
46+
print(f"Type Mismatch Error: {e}")
47+
except ValueError as e:
48+
print(f"Value Error: {e}")
49+
except Exception as e:
50+
print(f"An unexpected error occurred: {e}")

0 commit comments

Comments
 (0)