Search code examples
pythonclassmethodssuperboolean-expression

Why is the result of `bool` True here?


Here is my code:

class car():
    #defines a car model,speed,condition, and if you want to repair
    def __init__(self,model,speed):
        self.model = model
        self.speed = speed
        
    
    def roar(str = "vrooooooom"):
        print(str)
    
    def condition():
        user = bool(input('Is the car broken? True or False\n'))
        if user == True:
            print("Find local repair shop")
        else:
            print("No damage")

    def repair():
        wheels = ['O','O','O','O']
        if super().condition() == True:
            choice = input('Which one? 1-4\n')
            wheels[choice] = 'X'

When I call class.condition and put in False, I get 'find local repair shop' even though I want "no damage". As for repair, I feel like I'm using super() wrong.


Solution

  • That's not how it works. According to this post, Python considers any non-empty string as True. So when you enter False, it becomes a non-empty string which evaluates to True:

    Instead, you should do this.

    def condition():
        user = input('Is the car broken? True or False\n')
        if user == 'True':
            print("Find local repair shop")
        else:
            print("No damage")