0

This is the code i wrote for a guessing game in Python. It asks the user to guess the number between 1 and 1000. If the user is is correct or runs out of guesses, it will ask the user if he wants to play again. I used sys.exit to terminate the program. I was just wondering if there was a better way.

import random
import sys


def main():

    #Assigning the variables
    max_guess = 10
    num_guesses = 0
    max_number = 1000
    number = random.randint(1, max_number)
    guess = 0

    # Loop
    while guess != number:
        
        # Exceeding the guessing limit
        if max_guess == num_guesses:
            print("You have run out of guesses! The correct answer was " + str(number))
            if game_over():
                main()
            else:
                print("Thank you for playing!")
                sys.exit(0)

        # Asking for the user's guess
        guess = int(input("Guess the number between 1 and 1000: "))
        
        #Checking if the guess is appropriate
        if guess > 1000 or guess <= 0:
            print("The number has to be between 1 and 1000!")
            num_guesses -= 1
            
        #Checkng if the guess is too high or too low
        if 1000 > guess > number:
            print("Lower!")
        if 0 < guess < number:
            print("Higher!")

        # Winning the game
        if guess == number:
            print("Congratulations your guess is correct!")
            if game_over():
                main()
            else:
                print("Thank you for playing!")
                sys.exit(0)
        
        num_guesses += 1

# After the game is over
def game_over():
    while True:
        game_over = input("Would you like to play again(y/n)? ")
        if game_over == "y":
            return True
        elif game_over == "n":
            return False

main()

I initially used break function but it repeated "Guess the number between 1 and 1000: " even after it printed "Thank you for playing". I tried an endless amount of times but break didn't work. Is it okay to use sys.exit() in this situation, if not how can i use break?

Thanks!

chxnul
  • 1
  • 2
    You simply `return` from `main()`. And of cause you should use a loop instead of a recursive call to `main()`. – Klaus D. Apr 17 '23 at 07:31

1 Answers1

0

The problem is that main() recursively calls itself if the player chooses to play again. The previous call to main() has not returned yet, so a single break or return has no effect.

Try to restructure your code using a loop, as follows:

def play_game():
    # Play _one_ game, and return when it's over
    ...

def main():
    while True:
        play_game()
        if not play_again():
            break

def play_again():
    # Returns True if the player chooses to play again, False otherwise
    while True:
        play_again = input("Would you like to play again(y/n)? ")
        if play_again == "y":
            return True
        elif play_again == "n":
            return False

main()

Note that I renamed game_over to play_again. It's usually clearer to name functions based on what they do, not based on when they're called.

Thomas
  • 174,939
  • 50
  • 355
  • 478