devdbi

matriz_bugada.py

Sep 4th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import copy
  3.  
  4. class matriz(object):
  5.     matriz = []
  6.     is_quadrada = False
  7.     def __init__(self, lin:int, col:int):
  8.         self.linhas = lin
  9.         self.colunas = col
  10.  
  11.     #Método para gerar valores na matriz automaticamente
  12.     def gera_matriz(self):
  13.         for i in range(self.linhas):
  14.             self.matriz.append([])
  15.             for j in range(self.colunas):
  16.                 self.matriz[i].append([])
  17.                 self.matriz[i][j] = i*j
  18.  
  19.     #Método para preencher a matriz com valores fornecidos pelo usuário
  20.     def ler_matriz(self):
  21.         s = ""
  22.         print("Informe os valores para a matriz por gentileza: ")
  23.         for i in range(self.linhas):
  24.             self.matriz.append([])
  25.             for j in range(self.colunas):
  26.                 self.matriz[i].append([])
  27.                 #Trata erros caso o usuário informe algo diferente de número
  28.                 while True:
  29.                     s = input('matriz('+str(i)+','+str(j)+')')
  30.                     if s.isdigit():
  31.                         self.matriz[i][j] = int(s)
  32.                         break
  33.                     else:
  34.                         print("ERRO: Favor informe um número válido")
  35.  
  36.     def mostrar(self):
  37.         print(self)
  38.         if len(self.matriz) > 0:
  39.             print("\nMatriz: ")
  40.             for i in range(self.linhas):
  41.                 print(self.matriz[i])
  42.         else:
  43.             print("ERRO: Tamanho inválido para a matriz")
  44.  
  45. #Testes:
  46.  
  47. a = matriz(2,2)
  48. a.ler_matriz()
  49.  
  50. b = matriz(1,3)
  51. b.ler_matriz()
  52.  
  53. a.mostrar()
  54. b.mostrar()
Advertisement
Add Comment
Please, Sign In to add comment