-1

I am new to Python and writing a program for a number guessing game. Here's the code:

import random
import math

def guessing_game_func():
    name = input("Enter your name: ")
    print("Welcome ", name)
    lower_bound = 0
    upper_bound = 50
    #generating random number between 0 and 50
    random_number = random.randint(lower_bound, upper_bound)
    min_guessing_num = int(math.log(upper_bound - lower_bound + 1, 2))
    print("INSTRUCTION: Guess a number between 0 and 50"
          "\nHOT means you are close to the correct number and COLD means you are far from it")
    print("You have only ", round(min_guessing_num), "tries")

    #initializing number of guesses
    count = 0
    while count < min_guessing_num:
        count += 1
        guess = int(input("Guess a number: "))
        if random_number == guess:
            print("Congratulations. You guessed correctly after", count, "tries")
            break
        elif random_number > guess:
            print("Hot")
        elif random_number < guess:
            print("Cold")

        if count >= min_guessing_num:
            print("Fail! The number is", random_number)

    decision = input("Do you wish to play again? YES or NO").lower()
    while decision == "yes":
        guessing_game_func()
    #if decision == "yes":
    #guessing_game_func()
    if decision == "no":
     print("Close IDE")
guessing_game_func()

When I run this code, in the case where the user selects No, the game starts again but I do not want it to.

I just want it to print Close IDE and that should be it. However, the game starts again and I know this is because I am calling the function after it, but if I do not do that, then nothing will happen. What I mean by this is that, the code will run and all I'll see is Process finished with exit code 0

How do I fix this please?

  • 1
    There could be more problems, but the main thing is that you call the function again from within the function, resulting in recursion. Move the question outside of the function, into a loop that exits at “no” – Stefan Jan 18 '23 at 18:31
  • @Stefan The reason why I called the function again from within the function is because I need everything to happen again when the user says "Yes" and why I called it again at the bottom is because normally if you do not call a function, it will not "run", but this is however not working as I expected. Also, what you are telling me to do about the loop, I don't quite understand. Please could you explain again? –  Jan 18 '23 at 18:48
  • Yes the code works perfectly but I do not want the game to start again after the user writes "no". The only thing I want to happen is for it to print "close IDE" but that is not what happens. That is the issue I have. @MuzubulRehman –  Jan 18 '23 at 18:52
  • @Loni.for me its printing close IDE and exiting out of program. – Muzubul Rehman Jan 18 '23 at 18:54
  • once you do `while decision == "yes"` you always do it, there is no way to stop. Even if you say no in the "next" game unless you `sys.exit()` – JonSG Jan 18 '23 at 18:56
  • when you enter no after your guesses are exhausted .then only it prints close IDE. if you want to close it even before the guesses are exhausted. then you have to compare your guess with "no " everytime. – Muzubul Rehman Jan 18 '23 at 19:05
  • 1
    @MuzubulRehman Oh I see. For me the program starts again if I take type no after typing a yes once or more times and it just shows process finished with exit code 0 if I type no the first time the question is asked. –  Jan 18 '23 at 19:07
  • oh.got you now. – Muzubul Rehman Jan 18 '23 at 19:18
  • i have added an answer.it works now.even if you say no after saying yes once. – Muzubul Rehman Jan 18 '23 at 19:23

3 Answers3

2

Mad Physicist's answer covers the why does this happen and suggests a simple fix. However, I'd discourage you from using recursion to implement a replayed game. Instead, move the logic that decides whether to replay a game outside the function that implements the game logic.

def guessing_game_func():
    ...
    while count < min_guessing_num:
        ...

    # The function ends immediately after this while loop

# Now we're OUTSIDE the function

# Initialize decision to "yes", so the while loop is entered by default
decision = "yes"

while decision == "yes":
    guessing_game_func()
    decision = input("Do you wish to play again? YES or NO").lower()
    if decision == "no":
        print("Close IDE")

This way, you aren't limited to the recursion limit and can replay as many times as you like, since the next call of guessing_game_func only happens after the first call has ended.

I'm not sure why you want to print Close IDE only when "no" is entered -- what if decision is something other than "yes" and "no"? If you want to force decision to be one of those two options, you can pop it in another while loop, as shown in Asking the user for input until they give a valid response:

while decision == "yes":
    guessing_game_func()
    while True:
        decision = input("Do you wish to play again? YES or NO").lower()
        if decision in ("yes", "no"):
            break # Acceptable input, so break out of the infinite loop

# Close IDE can be unguarded by an `if`, because the only way to get here is to enter "no" when asked for decision
print("Close IDE")
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
1

The problem you have is with the loop that calls your function recursively:

decision = input("Do you wish to play again? YES or NO").lower()
while decision == "yes":
    guessing_game_func()
if decision == "no":
    print("Close IDE")

You have the recursive call which effectively implements a loop, and a while loop around that. When the inner call returns, remember that decision does not change within the outer function's while loop. That means that another call will be made and the loop will continue forever.

The simple solution is to decide whether you want to iterate via loop or recursion. I would recommend the former, but since you've already chosen the latter, just replace the while loop with a simple if:

decision = ""
while decision not in {"yes", "no"}:
    decision = input("Do you wish to play again? YES or NO").lower()
if decision == "yes":
    guessing_game_func()
else:
    print("Close IDE")
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
0
import random
import math

def guessing_game_func():
    name = input("Enter your name: ")
    print("Welcome ", name)
    lower_bound = 0
    upper_bound = 50
    #generating random number between 0 and 50
    random_number = random.randint(lower_bound, upper_bound)
    min_guessing_num = int(math.log(upper_bound - lower_bound + 1, 2))
    print("INSTRUCTION: Guess a number between 0 and 50"
          "\nHOT means you are close to the correct number and COLD means you are far from it")
    print("You have only ", round(min_guessing_num), "tries")

    #initializing number of guesses
    count = 0
    while count < min_guessing_num:
        count += 1
        guess = int(input("Guess a number: "))
        if random_number == guess:
            print("Congratulations. You guessed correctly after", count, "tries")
            break
        elif random_number > guess:
            print("Hot")
        elif random_number < guess:
            print("Cold")

        if count >= min_guessing_num:
            print("Fail! The number is", random_number)

    decision = input("Do you wish to play again? YES or NO").lower()
    while decision == "yes":
        guessing_game_func()
        break
    #if decision == "yes":
    #guessing_game_func()
    if decision == "no":
        print("Close IDE")


guessing_game_func()

Try this . I have added break after you call your function.now it works.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264