Quit function in python programming

Question:

I have tried to use the ‘quit()’ function in python and the spyder’s compiler keep says me "quit" is not defined

print("Welcome to my computer quiz")

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

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

the output keep says me "name ‘quit’ is not defined", how can i solve that problem?

Asked By: Şevket ÖLMEZ

||

Answers:

Invert the logic and play if the user answers yes. The game will automatically quit when it reaches the end of the file

print("Welcome to my computer quiz")

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

if (playing == "yes" ):
    print("Okay! Let's play :)")
Answered By: Thomas Weller

In Python, the quit function is not a built-in function, so you need to import it from the sys module first.

Here’s how you can fix your code:

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 :)")

Hope this helps!

Answered By: A-poc

There is no such thing as quit() in python. Python rather has exit(). Simply replace your quit() to exit().

print("Welcome to my computer quiz")

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

if (playing != "yes" ):
    exit()
    
print("Okay! Let's play :)")
Answered By: The Myth
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.