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?
CodePudding user response:
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")
CodePudding user response:
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")
CodePudding user response:
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.