0
votes

Coding a simple guessing game while learning Python basics. Game rules: The program repeatedly asks the user to input a guess, and increases the counter of guesses (guess_count) by 1. If the counter reaches a certain limit (guess_limit), the game says ("You loose"). If guessed correctly: ("You win")

I have two implementations and cannot understand, what design is conceptually more correct.

Variant 1: create a boolean variable out_of_guesses and set it initially to False

secret_word = "pythoniac"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False

The program then executes a WHILE loop, the WHILE loop finishes either if the word was guessed or if the guess limit was reached and out_of_guesses == True.

while guess != secret_word and not out_of_guesses:
    if guess_count < guess_limit:
        guess = input("Enter a guess: ")
        guess_count += 1
    else:
        out_of_guesses = True

After that, outside the WHILE loop, the programs checks, if out_of_guesses == True and prints ("You loose"). Else, it prints ("You win").

if out_of_guesses:
    print("You Lose!")
else:
    print("You Win!")

Variant 2: without additional boolean variable:

secret_word = "pythoniac"
guess = ""
guess_count = 0
guess_limit = 3

Within the WHILE loop, if the guess limit is reached, the program prints ("You lose") and breaks out of the loop with exit(). If the secret word was guessed, the program prints ("You win") outside the WHILE loop:

while guess != secret_word:
    if guess_count < guess_limit:
        guess = input("Enter a guess: ")
        guess_count += 1
    else:
        print("You loose!")
        exit()
print("You Win!")

The question: which variant is better as a general rule of thumb? To implement different outcomes within the WHILE loop and then break out of the WHILE loop with exit() / break? Or to introduce additional boolean variables to store different outcomes and implement these outcomes outside the WHILE loop?

many thanks in advance!

1
This depends on what else is going on in your program. It's not super-common in my experience to want to abruptly exit half way through the program, except via exceptions. In simple examples like this that may be fine, but often you have other cleanup that you want to do, or maybe a menu that you want the user to return to or something else. Also, variant two seems like an odd setup in this case. The user can leave the loop if they win, but will terminate if they lose? It seems to me like regardless of the outcome, both cases should be treated roughly the same. - Carcigenicate
@Carcigenicate thank you for your feedback regarding variant 2. I have rewritten the code so that both outcomes are covered in the while loop with exit() after printing ("You loose") or ("You win"). Regarding abrupt exit: I also had a feeling that is not very good idea. You confirmed my feeling. Thanks! - pavdemesh
I still wouldn't use exit. Again, it may work fine in simple scenarios, but you'll likely need to refactor the code to remove it if you add any complexity to your program later. There may be some cases where exiting half-way through the program is easier that trying let the script end normally, but I wouldn't use it simply to remove a Boolean variable from a loop's logic. - Carcigenicate
@Carcigenicate I agree with you. It really could be some other code after the while statement. And with exit - it would lead to errors - pavdemesh

1 Answers

0
votes

Here's another idea for you to consider: make it a function.

def do_game(word, max_tries=5):
    for _ in range(max_tries):
        answer = input("Enter a guess: ")
        if answer == word:
            return True
    return False

if do_game("pythoniac"):
    print("yes")
else:
    print("no")