【问题标题】:didnt understand a part of a code in python不理解python中的部分代码
【发布时间】:2021-08-14 09:38:08
【问题描述】:

你能告诉我以下python代码的第6行是怎么回事吗?

command = ""
started = False
while True:
    command = input("> ")
    if command == "start":
        if started:       #didnt understand this
            print("Car already started")
        else:
            started = True
            print("Car started ... ready to go")
    elif command == "stop":
        print("Car stopped")
    elif command == "help":
        print('''start - to start the
                 stop - to stop the car
                 quit - to exit''')
    elif command == "quit":
        break
    else:
        print("I don't get it")

【问题讨论】:

  • 你有什么不明白的>

标签: python-3.x loops if-statement while-loop


【解决方案1】:

第六行说如果开始是True 然后是print("Car already started")。它比说 if started == True 更简单,但与 started 评估为 TrueFalse 的含义相同

【讨论】:

  • 但是我们已经给变量开始值 False。所以当我们输入 if started: 不就意味着 if false。
  • 是的,但是一旦启动汽车并输入start 命令started 将等于False 然后if 语句将变为if true
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-10
  • 2021-06-20
  • 1970-01-01
  • 1970-01-01
  • 2019-10-05
  • 2019-04-23
  • 1970-01-01
相关资源
最近更新 更多