-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexe_041_classifying_triangles.py
146 lines (119 loc) · 4.55 KB
/
exe_041_classifying_triangles.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
"""
the Program receives from the USER the LENGTH of the THREE SIDES of a TRIANGLE,
and returns (displaying it) the TYPE of TRIANGLE, knowing that:
- three equal sides -> EQUILATERAL triangle
- two equal sides -> ISOSCELES triangle
- three different sides -> SCALENE triangle.
"""
# START Definition of FUNCTIONS
def valutaFloatPositive(numero):
countPoints = 0
for char in numero:
if ord(char) == 46:
countPoints += 1
if countPoints == 1 and numero != "." and valutaNumero(numero):
if isinstance(float(numero), float) and float(numero) > 0:
return True
else:
return False
def valutaNumero(numero):
if numero == "":
return False
countSigns = 0
for char in numero:
if ord(char) == 45 or ord(char) == 43:
countSigns += 1
if ((numero[0] == "+") or (numero[0] == "-")) and countSigns == 1 and \
numero != "-" and numero != "+" and numero != "-." and numero != "+.":
return True
elif numero[0].isdigit() and countSigns == 0:
return True
else:
return False
def valutaIntPositive(numero):
if numero.isdigit():
if numero != "0":
return True
return False
def verificaTriangle(side1, side2, side3):
if side1 >= (side2 + side3):
return False
if side2 >= (side1 + side3):
return False
if side3 >= (side2 + side1):
return False
return True
def typeTriangle(side1, side2, side3):
if side1 == side2:
if side2 == side3:
return "EQUILATERAL (three equal sides)"
return "ISOSCELES (two equal sides)"
elif side1 == side3:
if side3 == side2:
return "EQUILATERAL (three equal sides)"
return "ISOSCELES (two equal sides)"
elif side2 == side3:
if side3 == side1:
return "EQUILATERAL (three equal sides)"
return "ISOSCELES (two equal sides)"
else:
return "SCALENE (all different sides)"
# END Definition of FUNCTIONS
# Acquisition and Control of the DATA entered by the USER
print("Enter the THREE SIDES of the TRIANGLE.")
side1 = input("FIRST SIDE (cm): ")
side2 = input("SECOND SIDE (cm): ")
side3 = input("THIRD SIDE (cm): ")
side1FloatPositive = valutaFloatPositive(side1)
side1IntPositive = valutaIntPositive(side1)
side2FloatPositive = valutaFloatPositive(side2)
side2IntPositive = valutaIntPositive(side2)
side3FloatPositive = valutaFloatPositive(side3)
side3IntPositive = valutaIntPositive(side3)
while not(side1IntPositive or side1FloatPositive) or \
not(side2IntPositive or side2FloatPositive) or \
not(side3IntPositive or side3FloatPositive):
print("Incorrect entry. Try again.")
print("Enter the THREE SIDES of the TRIANGLE.")
side1 = input("FIRST SIDE (cm): ")
side2 = input("SECOND SIDE (cm): ")
side3 = input("THIRD SIDE (cm): ")
side1FloatPositive = valutaFloatPositive(side1)
side1IntPositive = valutaIntPositive(side1)
side2FloatPositive = valutaFloatPositive(side2)
side2IntPositive = valutaIntPositive(side2)
side3FloatPositive = valutaFloatPositive(side3)
side3IntPositive = valutaIntPositive(side3)
# Conversion STR -> FLOAT
side1 = float(side1)
side2 = float(side2)
side3 = float(side3)
# verification of the EXISTENCE of the TRIANGLE
if (side1IntPositive or side1FloatPositive) and \
(side2IntPositive or side2FloatPositive) and \
(side3IntPositive or side3FloatPositive):
triangoloVerified = verificaTriangle(side1, side2, side3)
while not(triangoloVerified):
print("ATTENTION: the SUM of TWO SIDES cannot be greater than the THIRD." +
"THIS TRIANGLE does NOT EXISTS. Try Again.")
print("Enter the THREE SIDES of the TRIANGLE.")
side1 = input("FIRST SIDE (cm): ")
side2 = input("SECOND SIDE (cm): ")
side3 = input("THIRD SIDE (cm): ")
side1FloatPositive = valutaFloatPositive(side1)
side1IntPositive = valutaIntPositive(side1)
side2FloatPositive = valutaFloatPositive(side2)
side2IntPositive = valutaIntPositive(side2)
side3FloatPositive = valutaFloatPositive(side3)
side3IntPositive = valutaIntPositive(side3)
if (side1IntPositive or side1FloatPositive) and \
(side2IntPositive or side2FloatPositive) and \
(side3IntPositive or side3FloatPositive):
side1 = float(side1)
side2 = float(side2)
side3 = float(side3)
triangoloVerified = verificaTriangle(side1, side2, side3)
# Identification of the TRIANGLE TYPE
typeTriangle = typeTriangle(side1, side2, side3)
# Displaying the RESULT
print("TYPE of TRIANGLE = " + typeTriangle)