20/11/2025, 17:55 Program using Exception Handling: Attempt review | LAP
es24ad171 VEERAMANI M 2028 Batch AI&DS C Section
Question 1
Correct
Mark 20.00 out of 20.00
Write a program to create a method that takes a string as input and throws an exception if the string does not contain vowels.
First Line of Input : String
For example:
Test Input Result
1 try No vowels in the String
2 Erode Vowels Exception : No. of Vowels is 3
Answer:
1 ▼ class NoVowelsException(Exception):
2 pass
3
4 ▼ def count_vowels(input_string):
5 vowels = 'aeiouAEIOU'
6 count = sum(1 for char in input_string if char in vowels)
7 ▼ if count == 0:
8 raise NoVowelsException("No vowels in the String")
9 ▼ else:
10 return count
11
12 ▼ try:
13 input_string = input().strip()
14 vowel_count = count_vowels(input_string)
15 print(f"Vowels Exception : No. of Vowels is {vowel_count}")
16 ▼ except NoVowelsException as e:
17 print(e)
Test Input Expected Got
1 try No vowels in the String No vowels in the String
2 Erode Vowels Exception : No. of Vowels is 3 Vowels Exception : No. of Vowels is 3
Passed all tests!
Correct
Marks for this submission: 20.00/20.00.
[Link]/mod/quiz/[Link]?attempt=49041&cmid=19824 1/3
20/11/2025, 17:55 Program using Exception Handling: Attempt review | LAP
Question 2
Correct
Mark 20.00 out of 20.00
Write a program that reads a list of integers from the user and throws an exception if any numbers are duplicates. Otherwise find the sum of
all elements
First Line of Input : No. of Elements
Second Line of Input : The Elements
For example:
Test Input Result
1 5 Sum of the Elements are 19
4
5
7
2
1
2 6 Duplicate Number Exception: Number 11 is Duplicate
22
11
42
11
2
5
3 6 Duplicate Number Exception: Number 11 and 42 are Duplicates
42
11
42
11
2
5
5 7 Duplicate Number Exception: Number 1,2 and 3 are Duplicates
1
1
2
2
3
3
4
Answer:
1 from collections import Counter
2
3 ▼ def process_numbers():
4 n = int(input())
5 numbers = [int(input()) for _ in range(n)]
6
7 counter = Counter(numbers)
8 duplicates = [num for num, count in [Link]() if count > 1]
9
10 ▼ if duplicates:
11 [Link]()
12 ▼ if len(duplicates) == 1:
13 print(f"Duplicate Number Exception: Number {duplicates[0]} is Duplicate")
14 ▼ elif len(duplicates) == 2:
15 print(f"Duplicate Number Exception: Number {duplicates[0]} and {duplicates[1]} are Duplicat
16 ▼ else:
17 *first_part, second_last, last = duplicates
18 prefix = ",".join(str(x) for x in first_part + [second_last])
19 print(f"Duplicate Number Exception: Number {prefix} and {last} are Duplicates")
20 ▼ else:
21 total = sum(numbers)
22 print(f"Sum of the Elements are {total}")
23
24 process_numbers()
[Link]/mod/quiz/[Link]?attempt=49041&cmid=19824 2/3
20/11/2025, 17:55 Program using Exception Handling: Attempt review | LAP
Test Input Expected Got
1 5 Sum of the Elements are 19 Sum of the Elements are 19
4
5
7
2
1
2 6 Duplicate Number Exception: Number 11 is Duplicate Duplicate Number Exception: Number 11 is Duplicate
22
11
42
11
2
5
3 6 Duplicate Number Exception: Number 11 and 42 are Duplicate Number Exception: Number 11 and 42 are
42 Duplicates Duplicates
11
42
11
2
5
5 7 Duplicate Number Exception: Number 1,2 and 3 are Duplicate Number Exception: Number 1,2 and 3 are
1 Duplicates Duplicates
1
2
2
3
3
4
Passed all tests!
Correct
Marks for this submission: 20.00/20.00.
[Link]/mod/quiz/[Link]?attempt=49041&cmid=19824 3/3