-
AutorEntradas
-
29 marzo, 2021 a las 8:37 pm #16575
Matias BenditkisParticipanteHola buenas tardes alguien sabe como podría agregarle a este código una excepción para que cuando el usuario ingrese un número negativo mande un error?
<div style=»color: #403f53; background-color: #fbfbfb; font-family: Consolas, ‘Courier New’, monospace; font-size: 14px; line-height: 19px; white-space: pre;»>
<div><span style=»color: #994cc3;»>def</span> <span style=»color: #4876d6; font-style: italic;»>suma</span><span style=»color: #111111;»>(</span><span style=»color: #4876d6;»>num1</span>, <span style=»color: #4876d6;»>num2</span><span style=»color: #111111;»>)</span>:</div>
<div> <span style=»color: #994cc3; font-style: italic;»>return</span> num1<span style=»color: #994cc3;»>+</span>num2</div>
<div><span style=»color: #994cc3;»>def</span> <span style=»color: #4876d6; font-style: italic;»>resta</span><span style=»color: #111111;»>(</span><span style=»color: #4876d6;»>num1</span>, <span style=»color: #4876d6;»>num2</span><span style=»color: #111111;»>)</span>:</div>
<div> <span style=»color: #994cc3; font-style: italic;»>return</span> num1<span style=»color: #994cc3;»>-</span>num2</div>
<div><span style=»color: #994cc3;»>def</span> <span style=»color: #4876d6; font-style: italic;»>multiplica</span><span style=»color: #111111;»>(</span><span style=»color: #4876d6;»>num1</span>, <span style=»color: #4876d6;»>num2</span><span style=»color: #111111;»>)</span>:</div>
<div> <span style=»color: #994cc3; font-style: italic;»>return</span> num1<span style=»color: #994cc3;»>*</span>num2</div>
<div><span style=»color: #994cc3;»>def</span> <span style=»color: #4876d6; font-style: italic;»>divide</span><span style=»color: #111111;»>(</span><span style=»color: #4876d6;»>num1</span>,<span style=»color: #4876d6;»>num2</span><span style=»color: #111111;»>)</span>:</div>
<div></div>
<div> <span style=»color: #994cc3; font-style: italic;»>try</span>:</div>
<div> <span style=»color: #994cc3; font-style: italic;»>return</span> num1<span style=»color: #994cc3;»>/</span>num2</div>
<div> <span style=»color: #994cc3; font-style: italic;»>except</span> <span style=»color: #4876d6;»>ZeroDivisionError</span>:</div>
<div> <span style=»color: #994cc3;»>print</span>(<span style=»color: #111111;»>»</span><span style=»color: #c96765;»>No se puede dividir entre 0</span><span style=»color: #111111;»>»</span>)</div>
<div> <span style=»color: #994cc3; font-style: italic;»>return</span> <span style=»color: #111111;»>»</span><span style=»color: #c96765;»>Operacion errónea</span><span style=»color: #111111;»>»</span></div>
<div><span style=»color: #994cc3; font-style: italic;»>while</span> <span style=»color: #bc5454;»>True</span>:</div>
<div> <span style=»color: #994cc3; font-style: italic;»>try</span>:</div>
<div> op1<span style=»color: #994cc3;»>=</span>(<span style=»color: #4876d6;»>int</span>(<span style=»color: #4876d6;»>input</span>(<span style=»color: #111111;»>»</span><span style=»color: #c96765;»>Introduce el primer número: </span><span style=»color: #111111;»>»</span>)))</div>
<div> op2<span style=»color: #994cc3;»>=</span>(<span style=»color: #4876d6;»>int</span>(<span style=»color: #4876d6;»>input</span>(<span style=»color: #111111;»>»</span><span style=»color: #c96765;»>Introduce el segundo número: </span><span style=»color: #111111;»>»</span>)))</div>
<div> <span style=»color: #994cc3; font-style: italic;»>break</span></div>
<div> <span style=»color: #994cc3; font-style: italic;»>except</span> <span style=»color: #4876d6;»>ValueError</span>:</div>
<div> <span style=»color: #994cc3;»>print</span>(<span style=»color: #111111;»>»</span><span style=»color: #c96765;»>Los valores introducidos no son correctos</span><span style=»color: #111111;»>»</span>)</div>
<div></div>
<div>operacion<span style=»color: #994cc3;»>=</span><span style=»color: #4876d6;»>input</span>(<span style=»color: #111111;»>»</span><span style=»color: #c96765;»>Introduce la operación a realizar (suma,resta,multiplica,divide): </span><span style=»color: #111111;»>»</span>)</div>
<div><span style=»color: #994cc3; font-style: italic;»>if</span> operacion<span style=»color: #994cc3;»>==</span><span style=»color: #111111;»>»</span><span style=»color: #c96765;»>suma</span><span style=»color: #111111;»>»</span>:</div>
<div> <span style=»color: #994cc3;»>print</span>(<span style=»color: #0c969b;»>suma</span>(<span style=»color: #4876d6;»>op1,op2</span>))</div>
<div><span style=»color: #994cc3; font-style: italic;»>elif</span> operacion<span style=»color: #994cc3;»>==</span><span style=»color: #111111;»>»</span><span style=»color: #c96765;»>resta</span><span style=»color: #111111;»>»</span>:</div>
<div> <span style=»color: #994cc3;»>print</span>(<span style=»color: #0c969b;»>resta</span>(<span style=»color: #4876d6;»>op1,op2</span>))</div>
<div><span style=»color: #994cc3; font-style: italic;»>elif</span> operacion<span style=»color: #994cc3;»>==</span><span style=»color: #111111;»>»</span><span style=»color: #c96765;»>multiplica</span><span style=»color: #111111;»>»</span>:</div>
<div> <span style=»color: #994cc3;»>print</span>(<span style=»color: #0c969b;»>multiplica</span>(<span style=»color: #4876d6;»>op1,op2</span>))</div>
<div><span style=»color: #994cc3; font-style: italic;»>elif</span> operacion<span style=»color: #994cc3;»>==</span><span style=»color: #111111;»>»</span><span style=»color: #c96765;»>divide</span><span style=»color: #111111;»>»</span>:</div>
<div> <span style=»color: #994cc3;»>print</span>(<span style=»color: #0c969b;»>divide</span>(<span style=»color: #4876d6;»>op1,op2</span>))</div>
<div><span style=»color: #994cc3; font-style: italic;»>else</span>:</div>
<div> <span style=»color: #994cc3;»>print</span> (<span style=»color: #111111;»>»</span><span style=»color: #c96765;»>Operación no contemplada</span><span style=»color: #111111;»>»</span>)</div>
<div><span style=»color: #994cc3;»>print</span>(<span style=»color: #111111;»>»</span><span style=»color: #c96765;»>Operación ejecutada. Continuación de ejecución del programa </span><span style=»color: #111111;»>»</span>)</div>
</div>6 abril, 2021 a las 5:22 pm #16642
JaimeParticipanteTen en cuenta que las excepciones no son condiciones, sino errores que pueden producirse en el programa cuando se está ejecutando; en este caso, que se intente dividir por 0 o que se introduzca un valor no numérico.
Que tú quieras que los valores sean enteros positivos es ya una decisión tuya como programador, pero eso no debería controlarse como una excepción, ya que el programa no va a dar error si se intentan realizar las operaciones con números negativos y/o decimales, con lo que si lo mejor es que uses un bucle while para obligar al usuario a introducir los valores que tú quieras.
-
AutorEntradas
- Debes estar registrado para responder a este debate.