Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import copy
- class matriz(object):
- matriz = []
- is_quadrada = False
- def __init__(self, lin:int, col:int):
- self.linhas = lin
- self.colunas = col
- #Método para gerar valores na matriz automaticamente
- def gera_matriz(self):
- for i in range(self.linhas):
- self.matriz.append([])
- for j in range(self.colunas):
- self.matriz[i].append([])
- self.matriz[i][j] = i*j
- #Método para preencher a matriz com valores fornecidos pelo usuário
- def ler_matriz(self):
- s = ""
- print("Informe os valores para a matriz por gentileza: ")
- for i in range(self.linhas):
- self.matriz.append([])
- for j in range(self.colunas):
- self.matriz[i].append([])
- #Trata erros caso o usuário informe algo diferente de número
- while True:
- s = input('matriz('+str(i)+','+str(j)+')')
- if s.isdigit():
- self.matriz[i][j] = int(s)
- break
- else:
- print("ERRO: Favor informe um número válido")
- def mostrar(self):
- print(self)
- if len(self.matriz) > 0:
- print("\nMatriz: ")
- for i in range(self.linhas):
- print(self.matriz[i])
- else:
- print("ERRO: Tamanho inválido para a matriz")
- #Testes:
- a = matriz(2,2)
- a.ler_matriz()
- b = matriz(1,3)
- b.ler_matriz()
- a.mostrar()
- b.mostrar()
Advertisement
Add Comment
Please, Sign In to add comment