Skip to content

Instantly share code, notes, and snippets.

@aduartem
Last active August 29, 2016 03:38
Show Gist options
  • Save aduartem/158c6f94edce68ddfbbb to your computer and use it in GitHub Desktop.
Save aduartem/158c6f94edce68ddfbbb to your computer and use it in GitHub Desktop.
Python - Clases y Objetos
#!/usr/bin/python
"""Ejemplo extraido desde el libro Python para todos de Raul Gonzalez Duque"""
class Coche:
def __init__(self, gasolina):
self.gasolina = gasolina
print "Tenemos", gasolina, "litros"
def arrancar(self):
if self.gasolina > 0:
print "Arranca"
else:
print "No arranca"
def conducir(self):
if self.gasolina > 0:
self.gasolina -= 1
print "Quedan", self.gasolina, "litros"
else:
print "No se mueve"
mi_coche = Coche(4)
mi_coche.arrancar()
mi_coche.conducir()
mi_coche.conducir()
mi_coche.conducir()
mi_coche.conducir()
mi_coche.arrancar()
print mi_coche.gasolina
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment