Skip to content

Commit a2403af

Browse files
authored
Merge pull request #4 from Jlopezjlx/master
Added solutions from @mirpulatov, @Jlopezjlx, @Prrrince for calculator
2 parents 3e3b830 + a2c1153 commit a2403af

File tree

3 files changed

+92
-22
lines changed

3 files changed

+92
-22
lines changed

assignments/calculator/solutions/Jlopezjlx/calc/calc.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
1-
print("CALCULADORA")
2-
print("OPCIONES: \n1-Suma\n2-Resta\n3-Multiplicar\n4-Dividir\n5-Restante")
1+
import maths
2+
loop = True
33

4-
opcion=int(input("Inserte opcion deseada: "))
5-
valor1 = int(input("Inserte primer valor: "))
6-
valor2 = int(input("Inserte segundo valor: "))
4+
while loop == True:
5+
6+
print("-----------------------------------------------------------------")
7+
print("OPCIONES: \n1-Suma\n2-Resta\n3-Multiplicar\n4-Dividir\n5-Restante")
8+
try:
9+
10+
opcion = int(input("Inserte opcion deseada: "))
11+
valor1 = int(input("Inserte primer valor: "))
12+
valor2 = int(input("Inserte segundo valor: "))
13+
except:
14+
print("Valuer Error")
15+
16+
try:
17+
if opcion == 1:
18+
resultado = maths.sumar(valor1,valor2)
19+
print("La suma de %d + %d es %d" % (valor1, valor2, resultado))
20+
elif opcion == 2:
21+
resultado = maths.restar(valor1,valor2)
22+
print("La resta de %d - %d es %d" % (valor1, valor2, resultado))
23+
elif opcion == 3:
24+
resultado = maths.multiplicacion(valor1,valor2)
25+
print("La multiplicacion de %d * %d es %d" % (valor1, valor2, resultado))
26+
elif opcion == 4:
27+
resultado = maths.division(valor1,valor2)
28+
print("La division de %d / %d es %d" % (valor1, valor2, float(resultado)))
29+
elif opcion == 5:
30+
resultado = maths.restante(valor1,valor2)
31+
print("El restante de %d y %d es %d" % (valor1, valor2, resultado))
32+
else:
33+
print("Error en las entradas, Bye")
34+
except ZeroDivisionError:
35+
print( "Error, no se puede dividir entre 0")
36+
except ValueError:
37+
print( "Value Error")
38+
except NameError:
39+
print( "Name Error")
740

8-
if opcion == 1:
9-
resultado = valor1 + valor2
10-
print("La suma de %d + %d es %d" %(valor1, valor2, resultado))
11-
elif opcion == 2:
12-
resultado = valor1 - valor2
13-
print("La resta de %d - %d es %d" %(valor1, valor2, resultado))
14-
elif opcion == 3:
15-
resultado = valor1 * valor2
16-
print("La multiplicacion de %d * %d es %d" %(valor1, valor2, resultado))
17-
elif opcion == 4:
18-
resultado = valor1 / valor2
19-
print("La division de %d / %d es %d" %(valor1, valor2, float(resultado)))
20-
elif opcion == 5:
21-
resultado = valor1 % valor2
22-
print("El restante de %d y %d es %d" %(valor1, valor2, resultado))
23-
else:
24-
print("Error en las entradas, Bye")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def sumar(a,b):
2+
return a + b
3+
4+
def restar(a,b):
5+
return a - b
6+
7+
def multiplicacion(a,b):
8+
return a * b
9+
10+
def division(a,b):
11+
return a / b
12+
13+
def restante(a,b):
14+
return a % b
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import unittest
2+
import maths
3+
4+
class testing_calc(unittest.TestCase):
5+
6+
7+
def test_sumar(self):
8+
result = maths.sumar(2,3)
9+
self.assertEqual(5,result)
10+
11+
12+
def test_restar(self):
13+
result = maths.restar(2,3)
14+
expected = 2 - 3
15+
self.assertEqual(expected,result)
16+
17+
18+
def test_multiplcacion(self):
19+
result = maths.multiplicacion(2,3)
20+
expected = 2 * 3
21+
self.assertEqual(expected, result)
22+
23+
24+
def test_division(self):
25+
result = maths.division(2,3)
26+
expected = 2 / 3
27+
self.assertEqual(expected, result)
28+
29+
30+
def test_restante(self):
31+
result = maths.restante(2,3)
32+
expected = 2 % 3
33+
self.assertEqual(expected, result)
34+
35+
36+
if __name__ == '__main__':
37+
unittest.main()
38+
39+
40+

0 commit comments

Comments
 (0)