【问题标题】:Quit function in python programmingpython编程中的退出函数
【发布时间】:2022-12-03 11:34:26
【问题描述】:

我曾尝试在 python 中使用“quit()”函数,但 spyder 的编译器一直说我“quit”未定义

print("Welcome to my computer quiz")

playing = input("Do you want to play? ")

if (playing != "yes" ):
    quit()
    
print("Okay! Let's play :)")

输出 keep 告诉我“name 'quit' is not defined”,我该如何解决这个问题?

【问题讨论】:

    标签: python function compiler-errors runtime-error syntax-error


    【解决方案1】:

    反转逻辑并在用户回答时播放是的.游戏到达文件末尾时会自动退出

    print("Welcome to my computer quiz")
    
    playing = input("Do you want to play? ")
    
    if (playing == "yes" ):
        print("Okay! Let's play :)")
    

    【讨论】:

      【解决方案2】:

      在 Python 中,quit 函数不是内置函数,因此您需要先从 sys 模块中导入它。

      以下是修复代码的方法:

      import sys
      
      print("Welcome to my computer quiz")
      
      playing = input("Do you want to play? ")
      
      if (playing != "yes" ):
          sys.exit()
          
      print("Okay! Let's play :)")
      

      希望这可以帮助!

      【讨论】:

        猜你喜欢
        • 2021-03-11
        • 1970-01-01
        • 2016-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-26
        • 2011-04-09
        • 1970-01-01
        相关资源
        最近更新 更多