forked from Vasanth2005kk/VGLUG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exception handling.py
50 lines (44 loc) · 1.81 KB
/
Exception handling.py
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
try:# TRY BLOCK
# Prompt the user for a number and convert it to an integer
num = int(input("Enter the number:"))
# Initialize an empty list to store sums of valid combinations
list_2 = []
# Iterate over possible values of i from 1 to num
for i in range(1, num + 1):
# Iterate over possible values of j from 1 to i
for j in range(1, i + 1):
# Iterate over possible values of k from 1 to j
for k in range(1, j + 1):
# Check if the product of i, j, and k equals the input number
if i * j * k == num:
# Print the combination found
print([i, j, k], "===>", end=" ")
# Calculate the sum of i, j, and k
list_1 = i + j + k
print(list_1) # Print the sum
# Append the sum to list_2
list_2.append(list_1)
# Initialize lists to store unique sums and their occurrences
list_3 = []
list_4 = []
# Iterate through the sums in list_2
for i in list_2:
# If the sum is not in list_3, add it
if i not in list_3:
list_3.append(i)
# If the sum is already in list_3 but not in list_4, add it to list_4
elif i not in list_4:
list_4.append(i)
# Print a separator line
print("<_______________________________________________________________>")
# Iterate through unique sums in list_4
for i in list_4:
# Print the answer if it is not empty
if i:
print("answer is: ===>", i)
except ValueError as error: # EXCEPTION BLOCK OR ERROR BLOCK
# Handle non-integer input errors
print("ERROR ==>", error)
finally:# FINALLY BLOCK
# Print a message indicating that the process is done
print("Done")