Last active
January 26, 2019 06:03
-
-
Save bay007/20a0a0229d2816e9c8884b8e58fb9b9b to your computer and use it in GitHub Desktop.
Clases -- Temas básicos.
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 random import randint | |
class Dado: | |
"""Simula un dado y mantiene su estado | |
--- Hay 3 clases de estilos de generar doc strings, | |
Sphinx, NumPy y Google. | |
""" | |
def __init__(self, num_caras=6): | |
self.numero_caras = num_caras | |
self.valor = None | |
def tirar_dado(self): | |
""" | |
Con éste método se tira un dado y arroja un calor entre 1 y el num_caras definido. | |
""" | |
self.valor = randint(1, self.numero_caras) # 4 | |
# Magic methods | |
def __repr__(self): | |
return f"Valor {self.valor}" | |
dado_1 = Dado(18) | |
print(dado_1) | |
print(dado_1.tirar_dado()) | |
print(dado_1.valor) | |
class Tablero: | |
""" | |
Estos son doc strings, con ellos se documenta que hace | |
una clase ó método. | |
""" | |
def __init__(self, numero_dados=2): | |
self.numero_dados = numero_dados | |
self.dados = [] | |
for _ in range(self.numero_dados): | |
self.dados.append(Dado()) | |
def tirar(self): | |
for dado in self.dados: | |
dado.tirar_dado() | |
tbl = Tablero() | |
tbl.tirar() | |
# for dado in tbl.dados: | |
# print(dado.valor) | |
print(tbl.dados) |
Author
bay007
commented
Jan 25, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment