1

I defined this class using spyder and to test it my teacher would create a new file just for testing and he would check if each method returned the desired result, can anyone tell me how to do that?

class Caminhos():
    
    def __init__(self,vi,vf):
        self._vi = vi
        self._vf = vf
        self._caminhoI = [vi, vf]
        
    def primeiroV(self):
        return self._caminhoI[0]
    
    def ultimoV(self):
        return self._caminhoI[-1]
    
    def comprimento(self):
        return len(self._caminhoI)

    def acrescentaV(self, v):
        self._caminhoI=self._caminhoI + [v]
    
    def pertenceQ(self, v):
        i = 0
        b = False
        while i < len(self._caminhoI) and not b:
            if self._caminhoI[i] == v:
                b = True
            i = i + 1
        return b
        
    def verticePos(self, v):
    #aqui preferia aplicar a pertenceQ ao V mas n sei bem a sintaxe
        i = 0              
        b = False
        while i < len(self._caminhoI) and not b:
            if self._caminhoI[i] == v:
                b = True
            i = i + 1
        if not b:
            print("Erro, o vertice nao pertence ao caminho")
        else:
            i = 0
            d = False
            while i < len(self._caminhoI) and not d:
                if self._caminhoI[i] == v:
                    d = True
                i = i + 1
            return i
        
    def mostra(self):
        return self._caminhoI
Guillermo Garcia
  • 528
  • 4
  • 11
João Dias
  • 11
  • 1

1 Answers1

0

After defining your class you can create a new file using Spyder to test each method. Create a new python file (you can call it Testing.py) make sure it is in the same folder as your file with the class previously defined and you can execute the next code to test your class:

# Assuming your file is called "Caminhos" and it is in the same folder as this python script
# Import every method of your class 
from Caminhos import *


# Instantiate class object using the built-in function __init__()
c1 = Caminhos(4,6)

# Use "." after the object to call each method

# See object using mostra() method
print(c1.mostra())

# Get first element using primeiroV() method
print("Primeiro elemento: {}".format(c1.primeiroV()))

# Get first element using ultimo() method
print("Ultimo elemento: {}".format(c1.ultimoV()))

# Get length using comprimento() method
print("Comprimento: {}".format(c1.comprimento()))

# Add element using comprimento() method
c1.acrescentaV(1)
print(c1.mostra())

# See if there is a number in object using pertenceQ() function
print(c1.pertenceQ(8))
print(c1.pertenceQ(4))

# See if there is a number in object using verticePos() function
print(c1.verticePos(4))
Guillermo Garcia
  • 528
  • 4
  • 11