How to specify the code to terminate if a specific word is entered as input?

Question:

the code is about a game like hangman. It has 3 input : guess_num, length, guess. It should terminate the whole program if in any of that input the word stop or exit is entered.my break statement only works for each part of the program. for example each input only terminates its part. how can i code this to terminate whole program if stop or exit is entered in any of that

while True:  # For repeating the game if user wants to
    print("Welcome to Guess-The-Letter game")  # Start of the game
    print("")  # Just and indent line
   

     # Asking for user to choose number of guess
        while True:
            num_guess = input("how many times do you want to guess [1-10]: ")
            if num_guess.lower() == "stop" or num_guess.lower() == "exit":
                print("Game Ends!")
                break
            try:
                num_guess = int(num_guess)
            except ValueError:
                print("Not a number!")
                continue  # Checking for non number input
            if num_guess < 1 or num_guess > 10:
                print("Out of Range")  # Checking for out of range input
            else:
                result1 = int(num_guess)  # Storing the value
                break  # Anything else would be in range that we want and will store the value and break the loop
                # Asking for user to choose length of word
        while True:
            length = input("please enter the length of the word [4-5]: ")
            if length.lower() == "stop" or length.lower() == "exit":
                print("Game Ends!")
                break
            try:
                length = int(length)
            except ValueError:
                print("Not a number!")
                continue  # Checking for non number input
            if length < 4 or length > 5:
                print("Out of Range")  # Checking for out of range input
            else:
                result2 = int(length)  # Storing the value
                break  # Anything else would be in range that we want and will store the value and break the loop
    
        # Choosing the words
        if result2 == 4:
            word = choice(word4)
        elif result2 == 5:
            word = choice(word5)
        guess_list = []  # List of guess letters
        sec_word = []  # the list for formatting the random word
        for i in range(len(word)):
            sec_word.append("*")  # To format the random word into ****
        guess = ""  # Defining nothing in variable for the first time of iteration
        print("Selecting the word...")  # Just a Display
        while result1 > 0:  # Start of the guessing game on the condition that guess left is greater than 0
            print("word is:", "".join(sec_word))  # Displaying the word
            # Displaying the remaining guess number
            print("Guess remaining:", result1)
            # Showing the guess for the previous time
            print("Previous guess:", guess)
            guess = input("Please guess a letter: ")  # Guessing the letter
            if guess in guess_list:
                # if user input a similar letter like previous letter
                print(guess, "has been guessed before", end=" ")
            guess_list.append(guess)  # Add the guess letters to a list
            if guess.lower() == "stop" or guess.lower() == "exit":
                print("Game Ends!")
                break  # Letting user end the game at anytime
            if guess in word:  # For the input that is correct
                if length == 4:  # Displaying the word letter by checking for each index for 4 letters words
                    if guess == word[0]:
                        sec_word[0] = guess
                    if guess == word[1]:
                        sec_word[1] = guess
                    if guess == word[2]:
                        sec_word[2] = guess
                    if guess == word[3]:
                        sec_word[3] = guess
                elif length == 5:  # Displaying the word letter by checking for each index for 5 letters words
                    if guess == word[0]:
                        sec_word[0] = guess
                    if guess == word[1]:
                        sec_word[1] = guess
                    if guess == word[2]:
                        sec_word[2] = guess
                    if guess == word[3]:
                        sec_word[3] = guess
                    if guess == word[4]:
                        sec_word[4] = guess
                print(guess, "is in the word!")
            if guess not in word:  # For the wrong inputs from user
                print(guess, "is not in the word! Try agin")
            print("")  # Just a space after each question
            if "".join(sec_word) == word:
                print("You win!")
                break  # if user guess the right word before end of the guess number
            result1 -= 1  # After each guess the remaining guess time drops 1 time
Asked By: Yashar Biabani

||

Answers:

You can use sys.exit() to terminate the program.

    import sys
    if guess.lower() == "stop" or guess.lower() == "exit":
        print("Game Ends!")
        sys.exit()
Answered By: mepro
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.