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

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

此问题已在此处找到答案

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

  1. class car():
  2. #defines a car model,speed,condition, and if you want to repair
  3. def __init__(self,model,speed):
  4. self.model = model
  5. self.speed = speed
  6. def roar(str = "vrooooooom"):
  7. print(str)
  8. def condition():
  9. user = bool(input('Is the car broken? True or False\n'))
  10. if user == True:
  11. print("Find local repair shop")
  12. else:
  13. print("No damage")
  14. def repair():
  15. wheels = ['O','O','O','O']
  16. if super().condition() == True:
  17. choice = input('Which one? 1-4\n')
  18. wheels[choice] = 'X'

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

0md85ypi

0md85ypi1#

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

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

相关问题