Skip to content

Commit b3d11de

Browse files
authored
Merge pull request #69 from lironmiz/unit3-Ex3.3.2
Create unit3_ex3.3.2.py
2 parents 9ec547a + c44c73d commit b3d11de

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

unit3_ex3.3.2.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# exercise 3.3.2 from unit 3
2+
'''
3+
Ido's birthday is coming up, and in honor of the occasion he wants to invite his friends to the birthday. Ido is interested in inviting only the people who are 18 years old or older. For this purpose, implement the following function.
4+
5+
def send_invitation(name, age):
6+
if int(age) < 18:
7+
print("under age")
8+
otherwise:
9+
print("You should send an invite to " + name)
10+
The function send_invitations accepts two parameters: name and age. If the age is less than 18, it prints an appropriate message on the screen.
11+
12+
Actually a custom exception called UnderAge.
13+
Right in the exception class the __str__ method. The method will return a string that tells the user that their age is less than 18. Add the present age of the invitee to the string, and in a few years he will be able to reach Ido's birthday.
14+
Throw away the exception you created in the code - for this purpose, replace code line number 3.
15+
Catch the exception in the code when it is thrown.
16+
To test, run the function with arguments 17 and 20.
17+
'''
18+
19+
class UnderAge(Exception):
20+
def __init__(self, age):
21+
self.age = age
22+
23+
def __str__(self):
24+
return f"Your age is less than 18. You are currently {str( self.age)} years old. In a {str(18 - self.age)} you will be able to join Ido's birthday celebration."
25+
26+
27+
def send_invitation(name, age):
28+
if int(age) < 18:
29+
raise UnderAge(age)
30+
else:
31+
print("You should send an invite to " + name)
32+
33+
34+
try:
35+
send_invitation("John", 17)
36+
send_invitation("Anna", 20)
37+
except UnderAge as e:
38+
print(e)

0 commit comments

Comments
 (0)