Error '' 'bool' object is not subscriptable ''

1

I do not want to sound demanding or anything so I just want to ask you a huge favor, I try to make a program that has 20 rooms (they are all those s1 , s2 etc with true ) tell me if they are busy or no, if ' 'numero de habitación''==True is free if false is occupied , but when trying to execute the code the message appears, 'bool' object is not subscriptable , I'm just learning to use python.

s1=True
s2=True
s3=True
s4=True
s5=True
s6=True
s7=True
s8=True

f1=True
f2=True
f3=True
f4=True
f5=True
f6=True
f7=True
f8=True

p1=True
p2=True
p3=True
p4=True
class reservas:
    def __init__(self):
        self.Hs={s1,s2,s3,s4,s5,s6,s7,s8}
        self.Hf={f1,f2,f3,f4,f5,f6,f7,f8}
        self.Hp={p1,p2,p3,p4}
    def Estado(self):
        print("ingrese el tipo de habitacion")
        print("1) Sencilla")
        print("2) Familiar")
        print("3) Premium")
        self.x=int(input("------>"))
        if self.x==1:
            for i in self.Hs:
                c=0
                if i[c]==True:
                    self.a="Disponible"
                else:
                    self.a="Ocupada"
                print("La habitacion"+str(c),"esta",self.a)
                c=c+1
        elif self.x==2:
            for i in self.Hs:
                c=0
                if i[c]==True:
                    self.a="Disponible"
                else:
                    self.a="Ocupada"
                print("La habitacion"+str(c),"esta",self.a)
                c=c+1
        elif self.x==3:
            for i in self.Hs:
                c=0
                if i[c]==True:
                    self.a="Disponible"
                else:
                    self.a="Ocupada"
                print("La habitacion"+str(c),"esta",self.a)
                c=c+1
        elif self.x>3:
            print("Ingrese un valor correcto")

a=reservas()
a.Estado()
    
asked by Michael Navarrete 26.11.2017 в 19:19
source

1 answer

1

The error is because you are trying to iterate over a boolean object or trying to access the indexed one:

>>> s1 = True
>>> s1[0]
Traceback (innermost last):
  File "<stdin>", line 1, in <module>
TypeError: 'bool' object is not subscriptable

>>> for e in s1:
...     pass
... 
Traceback (innermost last):
  File "<stdin>", line 1, in <module>
TypeError: 'bool' object is not iterable

However, your attributes Hs , Hf and Hp are currently sets with a single boolean object inside: {True} . Sets use hash tables and their elements can not be repeated. When you make self.Hs={s1,s2,s3,s4,s5,s6,s7,s8} since all boolean variables are with value True , that is, they all have the same hash (1), only one remains in the set.

A good option, which I think is what you really want, is to use dictionaries where the key is a string identifying the room and the value of the state of it.

class Reservas:
    def __init__(self):
        self.Hs={"s1": True, "s2": True, "s3": True, "s4": True, "s5": True,
                 "s6": True, "s7": True, "s8": True}
        self.Hf={"f1": True, "f2": True, "f3": True, "f4": True, "f5": True,
                 "f6": True, "f7": True, "f8": True}
        self.Hp={"p1": True, "p2": True, "p3": True, "p4": True}

    def estado(self):
        print("ingrese el tipo de habitacion")
        print("1) Sencilla")
        print("2) Familiar")
        print("3) Premium")
        self.x=int(input("------>"))
        if self.x==1:
            for habitacion, estado in self.Hs.items():
                print('{} --> {}.'.format(habitacion,
                                          "Libre" if estado else "Ocupada"))

To change the status of any room simply access using your password and change its value:

self.Hs["s2"] = False
    
answered by 26.11.2017 / 19:40
source