如何解决为什么这里`bool` 的结果是True?
这是我的代码:
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']
if super().condition() == True:
choice = input('Which one? 1-4\n')
wheels[choice] = 'X'
当我调用 class.condition 并输入 False 时,即使我想要“没有损坏”,我也会得到“找到当地维修店”。至于修复,我感觉我用super()错了。
解决方法
这不是它的工作原理。根据{{3}},
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")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。