Objectivo
Desarrolla una clase Python llamada "Table". La clase debe incluir un constructor para inicializar el ancho y la altura de la tabla. También debe tener un método llamado "ShowData" que imprima las dimensiones de la tabla. Luego, crea una matriz con 10 tablas, cada una con dimensiones aleatorias que van desde 50 cm hasta 200 cm. Finalmente, muestra la información de cada tabla en la matriz.
Ejemplo Ejercicio Python
Mostrar Código Python
import random
class Table:
def __init__(self, width, height):
self.width = width
self.height = height
def ShowData(self):
print(f"Table dimensions - Width: {self.width} cm, Height: {self.height} cm")
if __name__ == "__main__":
tables = []
for _ in range(10):
width = random.randint(50, 200)
height = random.randint(50, 200)
table = Table(width, height)
tables.append(table)
print("Table Information:")
for table in tables:
table.ShowData()
Output
Table Information:
Table dimensions - Width: 157 cm, Height: 104 cm
Table dimensions - Width: 158 cm, Height: 112 cm
Table dimensions - Width: 172 cm, Height: 60 cm
Table dimensions - Width: 103 cm, Height: 138 cm
Table dimensions - Width: 141 cm, Height: 173 cm
Table dimensions - Width: 69 cm, Height: 148 cm
Table dimensions - Width: 187 cm, Height: 138 cm
Table dimensions - Width: 56 cm, Height: 148 cm
Table dimensions - Width: 110 cm, Height: 114 cm
Table dimensions - Width: 183 cm, Height: 161 cm
Código de Ejemplo copiado
Comparte este ejercicios Python