Created
September 27, 2023 15:56
-
-
Save j2deme/d51b197ef73b059839cd26c4eb910304 to your computer and use it in GitHub Desktop.
Contador de Pasos POO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multipledispatch import dispatch # pip install multipledispatch | |
class ContadorPasos: | |
pasos = -1 | |
def __init__(self, pasos=0): | |
self.pasos = pasos | |
def __del__(self): | |
pass | |
# print("Termine con", self.pasos, "pasos") | |
@dispatch() | |
def contar(self): # type:ignore | |
self.pasos += 1 | |
@dispatch(int) | |
def contar(self, pasos): # type: ignore | |
self.pasos += pasos | |
@dispatch(int, int) | |
def contar(self, pasos, saltos): | |
self.pasos += pasos + (2 * saltos) | |
def saltarAdelante(self): | |
self.contar() # type:ignore | |
self.contar() # type:ignore | |
# Toma la cantidad de pasos y devuelve la distancia en metros | |
def calcularDistancia(self, pasos): | |
# 1m = 0.7 pasos | |
return pasos * 0.7 | |
# Toma la distancia en metros y devuelve la cantidad de pasos | |
def calcularPasos(self, distancia): | |
return round(distancia / 0.7) | |
def __add__(self, cuenta): | |
return ContadorPasos(self.pasos + cuenta.pasos) | |
def __sub__(self, cuenta): | |
return ContadorPasos(self.pasos - cuenta.pasos) | |
def getPasos(self): | |
return self.pasos | |
def __str__(self): | |
return str(self.pasos) + " 👟" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ContadorPasos import ContadorPasos | |
def main(): | |
miContador = ContadorPasos() | |
print(miContador.pasos) | |
distancia1 = int(input("¿Que distancia vas a caminar?")) | |
pasos1 = miContador.calcularPasos(distancia1) | |
print("Para caminar", distancia1, "metros, se ocupan", pasos1, "pasos") | |
for i in range(0, pasos1): | |
miContador.contar() # type: ignore | |
print(miContador.pasos) | |
miContador.contar(5) # type: ignore | |
print(miContador.pasos) | |
miContador.contar(7, 2) | |
print(miContador.pasos) | |
# pasos2 = int(input("Pasos? ")) | |
# distancia2 = miContador.calcularDistancia(pasos2) | |
# print("Caminar", pasos2, "pasos, nos permite avanzar", distancia2, "metros") | |
print("SOBRECARGA DE OPERADORES") | |
cuenta1 = ContadorPasos(10) | |
cuenta2 = ContadorPasos(20) | |
cuenta3 = cuenta1 + cuenta2 | |
cuenta2 = cuenta2 - cuenta1 | |
print(cuenta3) | |
print(cuenta2) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment