I know there is a solution to the number guessing game in python. I’ve seen the code here too. and as a continuation how can I add a function(or anything else) to prompt the user- after successfully guessing it- if he wants to continue or not if not halt it. I know the code don’t know how to add it to code.
import random def play_game(): print("Enter the upper limit for the range of numbers: ") limit = int(input()) number = random.randint(1, limit) print("I'm thinking of a number from 1 to " + str(limit) + "n") count = 1 while True: guess = int(input("Your guess: ")) if guess < number: print("Too low.") elif guess > number: print("Too high.") elif guess == number: print("You guessed it in " + str(count) + " tries.n") return count+=1
so here is what we had in this forum but how can I add this to what I have above :
ask = input('do you want to continue? y stands for yes and n for discontinuing') if ask=="y": UserGuess=int(input('Enter youre desired Number: ')) else: return
Include it in a
while loop outside the function. This way, you will get recursion out of the way.
def play_game(): .... .... count+=1 play_game() while True: ask = input('do you want to continue? y stands for yes and n for discontinuing') if ask=="y": play_game() else: break