Search code examples
python-3.xclassobjectqueuepriority-queue

My vip variable does not take False as a value and always defaults to True


Below is the code to return either a VIP customer or an ordinary customer, the list does not take vip as a False value and always takes True.

Class DonutQueue():
    def arrive(self,name,VIP):

        self.name = name
        self.vip = VIP
        if self.vip==True:
            self.queue2.append(self.name)
            return self.queue2
        else:
            self.queue.append(self.name)
            return self.queue


    def next_customer(self):

        while not self.queue2== []:
            if not self.queue2==[]:
                return self.queue
            else:
                return self.queue2

def main():

    n = int(input("Enter the number of customers you want to add"))
    for i in range(0,n):
        name = input("Enter their name")
        vip= bool(input("Are they a VIP"))
        DonutQueue.arrive(name,VIP)
    print (DonutQueue().next_customer())

Below is the output:

Enter the number of customers you want to add2
Enter their name John
Are they a VIP False
Enter their name Wick
Are they a VIP True
None

Why am I getting None as the output and my value always takes True when I input False.

Below is the debugger values:

i:0
n:2
name: John
vip: True

Solution

  • The Python input() method reads user input and returns it as string.

    Python bool() will always return a True value for a non-empty string and False only if a string is empty.

    In order to get correct the value for vip as a bool you have to manually check the input string

    vipString = input("Are they a VIP")
    vip = True if vipString == 'yes' else False