-
Notifications
You must be signed in to change notification settings - Fork 0
/
as.py
156 lines (122 loc) · 3.9 KB
/
as.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
import sys
import time
def run():
USERINPUT_ERROR = False
INTERNAL_ERROR = False
INTERNAL_ERROR_COUNTER = 0
ex1=0
ex2=0
n1,n2,USERINPUT_ERROR=userInput()
if USERINPUT_ERROR:
sys.exit("Too many wrong inputs")
elif not USERINPUT_ERROR:
try:
sum =computerSummation(n1,n2)
print("Using for loop:= Sum from ",n1," to ",n2,"is:",sum)
except Exception as e:
time.sleep(0.01)
INTERNAL_ERROR = True
INTERNAL_ERROR_COUNTER+=1
ex1 = e
try:
sum = mathSummation(n1,n2)
print("Using math equations:= Sum from ",n1," to ",n2,"is:",sum)
except Exception as e:
time.sleep(0.01)
INTERNAL_ERROR = True
INTERNAL_ERROR_COUNTER+=1
ex2 = e
if INTERNAL_ERROR:
print("Program finished with ",INTERNAL_ERROR_COUNTER," error(s).")
print(ex1,"\n",ex2)
elif not INTERNAL_ERROR:
print("See you ;)")
def ValidateInput(chars):
try:
number_check = int(chars)
return True,number_check
except BaseException as e:
return False,0
def checkInput(n1:int,n2:int):
if n2 > n1:
return True
elif n1 > n2:
return False
else:
return False
def userInput():
ERROR_COUNTER = 0
IS_VALID_NUMBER = False
ERROR_FLAG = False
IS_VALID_INPUT = False
ERROR_COUNTER2 = 0
while(not IS_VALID_INPUT and ERROR_COUNTER2 < 2 ):
while(not IS_VALID_NUMBER and ERROR_COUNTER < 2):
print("Start position ?")
n1 = input()
IS_VALID_NUMBER,n1 = ValidateInput(n1)
if not IS_VALID_NUMBER:
print("Not a valid numbr")
ERROR_COUNTER += 1
if(not IS_VALID_NUMBER):
ERROR_FLAG = True
return 0,0,ERROR_FLAG
ERROR_COUNTER = 0
IS_VALID_NUMBER = False
while((not IS_VALID_NUMBER) and ERROR_COUNTER < 2):
print("Last position ?")
n2 = input()
IS_VALID_NUMBER,n2 = ValidateInput(n2)
if not IS_VALID_NUMBER:
print("Not a valid number")
ERROR_COUNTER += 1
if(not IS_VALID_NUMBER):
ERROR_FLAG = True
return 0,0,ERROR_FLAG
ERROR_COUNTER = 0
IS_VALID_NUMBER = False
#ERROR_FLAG = False
#return n1,n2,ERROR_FLAG
if checkInput(n1,n2):
IS_VALID_INPUT = True
ERROR_FLAG=False
return n1,n2,ERROR_FLAG
elif not checkInput(n1,n2):
IS_VALID_INPUT = False
IS_VALID_NUMBER = False
ERROR_FLAG = True
ERROR_COUNTER2+=1
print("First position bigger than last position?")
if not IS_VALID_INPUT:
return 0,0,ERROR_FLAG
def computerSummation(n1:int,n2:int):
sum=0
if n1 > n2:
raise Exception("start position > end position")
try:
if n1 > 9999999 or n2 > 9999999:
raise BaseException()
for i in range(n1,n2+1):
sum+=i
except BaseException as e:
raise Exception("Summation error")
return sum
def mathSummation(n1:int,n2:int):
if n1 > n2:
raise Exception("start position > end position")
try:
n =(n2 - n1)+1
sum = int((n/2)*(n1+n2))
except BaseException as e:
raise Exception("Math error")
return sum
if __name__ == "__main__":
n1=0
n2=0
print("Sum of consecutive number")
try:
run()
except BaseException as e:
print(e)
sys.exit()