为什么这里的'bool'结果是真的?

e5njpo68  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(408)

此问题已在此处找到答案

在python中从字符串转换为布尔值((31个答案)
“自我”这个词的目的是什么((26个答案)
“super”在python中的作用是什么super().\uuuu init\uuuuuuu()和显式超类\uuuuu init\uuuuuuu()之间的差异(11个答案)
昨天关门了。
这是我的密码:

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'

当我调用class.condition并输入false时,我会得到“查找本地维修店”,尽管我希望“无损坏”。至于修理,我觉得我用super()是错误的。

0md85ypi

0md85ypi1#

这不是它的工作原理。根据这篇文章,python认为任何非空字符串都是 True . 所以当你进去的时候 False ,它将成为一个非空字符串,其计算结果为 True :
相反,你应该这样做。

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

相关问题