-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_sum_even.py
More file actions
61 lines (51 loc) · 1.59 KB
/
list_sum_even.py
File metadata and controls
61 lines (51 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'''
Program : List Even Sum Program
Description:
This program creates a list of integers provided by the user
and calculates the sum of even numbers from that list.
Features :
- Valide user input
- Prevents negative numbers
- Handles invalid inputs using the exception handling
- Calculate the sum of even numbers in the list
Concepts used :
- Functions
- Exception handling (try/except)
- Input validation
- Loops
- Genetal expression
- Type checking
Author : Ryo
Date : 2026
'''
def add (elements) :
if not isinstance(elements,list) :
raise TypeError('List Expected')
return sum(i for i in elements if i%2==0)
def to_eliminate_negative_integers(prompt):
while True :
try :
input_number=int(input(prompt))
if input_number<0 :
raise ValueError('Input value must not be a negative integer')
return input_number
except ValueError :
print('Enter non negative number')
def list_creation (value):
the_list =[]
for i in range(value):
while True :
try:
b=int(input(f'Enter the {i+1} element : '))
if b<0:
raise ValueError('Negative integers are not allowed')
the_list.append(b)
break
except ValueError:
print('Enter the integer')
return the_list
if __name__ =="__main__" :
input_number = to_eliminate_negative_integers(f'How manu values needed to be added in the list : ')
elements=list_creation(input_number)
print('The list is : ',elements)
print(add(elements))