Parking class in Python

Asked

Viewed 479 times

0

I’m learning to code in Python.

Right now I need to implement a class that has the following features:

A class called parking, which simulates the operation of a car park.

-The class receives an integer and determines the capacity of the park

-The class returns an object with the following methods: Entra(): corresponds to the entrance of a car

-Sai(): corresponds to the exit of a car

-Seats(): indicates the number of free parking spaces.

I’m having trouble creating the code.

I can’t seem to get The class receives an integer and determines the capacity of the park

Code

  • You can edit your question to include what you’ve tried and where it didn’t work?

  • I already put the code there. however I am not being able to input the integer.

1 answer

1


Your parking control class can be something like:

class Estacionamento:

    def __init__( self, max_vagas ):
        self.max_vagas = max_vagas
        self.vagas_ocupadas = 0

    def maximo( self ):
        return self.max_vagas

    def disponiveis( self ):
        return self.max_vagas - self.vagas_ocupadas

    def ocupadas( self ):
        return self.vagas_ocupadas

    def entra( self ):
        if( self.vagas_ocupadas == self.max_vagas ):
            raise Exception('Estacionamento estah lotado!')
        self.vagas_ocupadas += 1

    def sai( self ):
        if( self.vagas_ocupadas == 0 ):
            raise Exception('Estacionamento estah vazio!')
        self.vagas_ocupadas -= 1

Testing:

n = int(input('Entre com a lotacao maxima do estacionamento: '))

e = Estacionamento(n)

e.entra()
e.entra()
e.entra()
e.entra()
e.sai()

print( e.maximo() )
print( e.disponiveis() )
print( e.ocupadas() )

Exit:

Entre com a lotacao maxima do estacionamento: 20
20
17
3
  • OK!! a lot of thanks for the help. But how do I ask the user for a whole!?

  • That is -The class receives a whole and determines the capacity of the park

  • 1

    Try something like: n = int(input('Entre com a lotacao maxima do estacionamento: '))

  • I did it! Before declaring the class. but this does not update the places!

  • 1

    @Joaopeixotofernandes: feito!

  • thanks so much for the availability and help!! It works! thanks

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.