forked from CloudLabsAI-Azure/Code-Generation-Refactoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum_elements.py
More file actions
40 lines (35 loc) · 980 Bytes
/
sum_elements.py
File metadata and controls
40 lines (35 loc) · 980 Bytes
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
MAX = 100
def calculate_sum(arr):
return sum(arr)
def get_number_of_elements():
while True:
try:
n = int(input("Enter the number of elements (1-100): "))
if 1 <= n <= MAX:
return n
else:
print("Invalid input. Please provide a number ranging from 1 to 100.")
except ValueError:
print("Invalid input. Please enter a valid integer.")
def get_elements(n):
arr = []
print(f"Enter {n} integers:")
for _ in range(n):
while True:
try:
arr.append(int(input()))
break
except ValueError:
print("Invalid input. Please enter a valid integer.")
return arr
def main():
try:
n = get_number_of_elements()
arr = get_elements(n)
total = calculate_sum(arr)
print("Sum of the numbers:", total)
except KeyboardInterrupt:
print("\nProgram terminated by user.")
exit(1)
if __name__ == "__main__":
main()