为什么这里 `bool` 的结果是 True?
Why is the result of `bool` True here?
这是我的代码:
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 时,我得到 'find local repair shop',即使我想要“无损坏”。至于修复,我感觉我用错了super()
事情不是这样的。根据this post,
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")
这是我的代码:
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 时,我得到 'find local repair shop',即使我想要“无损坏”。至于修复,我感觉我用错了super()
事情不是这样的。根据this post,
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")