Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- """ Script que pide un dos nros y devuelve el cociente entre ellos
- Ejs:
- >>> Ingrese el dividendo: 25
- >>> Ingrese el divisor: 5
- >>> El cociente entre 25 y 5 es 5.0
- Ayuda:
- ValueError: intenta pasar a entero una cadena que no es número
- ZeroDivisionError: cuando intenta dividir por cero
- try:
- .....
- except <error>:
- ......
- """
- def dividir(num1, num2):
- if num2 == 0:
- return "Lo siento no se puede dividir por 0"
- else:
- return num1 / num2
- def ingresar_numero(tipo):
- """
- .....
- Ej:
- Si llamo a la función:
- a = ingresar_numero("numero a")
- al ejecutarla se verá:
- >>> Ingrese el numero a:
- """
- while True:
- try:
- numero = float(input(f"Ingrese el {tipo}: "))
- except ValueError:
- print("No has ingresado un nùmero")
- else:
- if tipo == "divisor" and not numero:
- print("Debes ingresar un número distinto de cero")
- else:
- return numero
- numero1 = ingresar_numero("dividendo")
- numero2 = ingresar_numero("divisor")
- a = ingresar_numero("numero a")
- print(f"El cociente entre {numero1} y {numero2} es {dividir(numero1, numero2)}")
Advertisement
Add Comment
Please, Sign In to add comment