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
37 lines (28 loc) · 952 Bytes
/
sum_elements.py
File metadata and controls
37 lines (28 loc) · 952 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
#A poorly written example of a program in Python. It prompts the user for the number of elements to sum, takes those integers as input, and handles some basic error cases
MAX = 100
def calculate_sum(arr):
result = 0
for num in arr:
result += num
return result
def main():
try:
n = int(input("Enter the number of elements (1-100): "))
if not 1 <= n <= MAX:
print("Invalid input. Please provide a digit ranging from 1 to 100.")
exit(1)
arr = []
print(f"Enter {n} integers:")
for _ in range(n):
try:
arr.append(int(input()))
except ValueError:
print("Invalid input. Please enter valid integers.")
exit(1)
total = calculate_sum(arr)
print("Sum of the numbers:", total)
except KeyboardInterrupt:
print("\nProgram terminated by user.")
exit(1)
if __name__ == "__main__":
main()