Python Forum
[split] How to ask Smart Questions (thread title expansion)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] How to ask Smart Questions (thread title expansion)
#1
class Puerta:
    def _init_(self):
self.abierta = False  # Estado inicial: cerrada
        def abrir(self):
        if not self.abierta:
            self.abierta = True
            print("La puerta se ha abierto")
        else:
            print("La puerta ya estaba abierta")
    
    def cerrar(self):
        if self.abierta:
            self.abierta = False
            print("La puerta se ha cerrado")
        else:
            print("La puerta ya estaba cerrada")
    
    def estado(self):
        return "Abierta" if self.abierta else "Cerrada"
    
    def alternar(self):
        self.abierta = not self.abierta
        estado = "abierta" if self.abierta else "cerrada"
        print(f"La puerta se ha {estado}")

# Uso básico
puerta1 = Puerta()
print(f"Estado inicial: {puerta1.estado()}")

puerta1.abrir()
print(f"Estado: {puerta1.estado()}")

puerta1.cerrar()
print(f"Estado: {puerta1.estado()}")

puerta1.alternar()  # Abre si está cerrada, cierra si está abierta
print(f"Estado: {puerta1.estado()}")
buran write Dec-25-2025, 05:16 AM:
BbCode tags added
Reply
#2
Do you have question?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
¿Cual es, exactamente, la pregunta tan inteligente? ¿Tal vez esta abajo?

Quote:¿En cual estado está la puerta?
Big Grin Big Grin Big Grin

No need to get so complicated:

class Puerta:    
    
    def __init__(self, estado = ""):
        self.estado = estado       

    def alternar(self):
        if self.estado == 'cerrada':
            self.estado = 'abierta'
            print('La puerta se abrió sola.')
        elif self.estado == 'abierta':
            self.estado =  'cerrada'
            print('La puerta se cerró sola.')
        else:
            print('La puerta no está en buen estado, porfa hacer algo... ')


puerta1 = Puerta('cerrada')
¡Feliz Navidad! (Got to get back to the kitchen!)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [split] How to ask Smart Questions (thread title expansion) darkuser 4 2,311 Nov-11-2024, 01:27 PM
Last Post: deanhystad
  Counting Number of Element in a smart way quest 2 3,585 Nov-09-2020, 10:24 PM
Last Post: quest
  [split] New thread/topic rajp1497 1 2,898 Sep-24-2020, 01:55 AM
Last Post: micseydel
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 22,028 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE
  Build class to make a Smart list - trying icm63 7 5,733 Mar-28-2019, 08:53 PM
Last Post: icm63
  How to change font size of chart title and axis title ? thrupass 5 22,431 Mar-30-2018, 04:02 PM
Last Post: DrFunn1
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 57,322 Nov-25-2017, 06:21 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020