Skip to content
← Back to Community
Random Number Game
Profile icon

How do we can we break out a while loop if we can guess the number right? By the way this is my code.
import random
Facts = True
while Facts == True:
def guess():
num = int(input("Please enter your guess"))
return num
print("Welcome to the number guessing game")

print("The objective is to guess the number I'm thinking of.")
print("I will give you clues after your first guess.")
secretNumber = random.randint(1,100)
print("I have thought of a number from 1-100")
print(" ")
numGuessed = guess()
if numGuessed == secretNumber:
print("Well done you guessed it right")
Facts = False
elif numGuessed < secretNumber:
print("Guess is too low, guess higher!")
print("Guess is too high, guess lower!")

Profile icon
Profile icon
Profile icon

You can exit a while loop by putting break after they guess the number correctly

Please upvote this comment if it was helpful or useful in any way

Profile icon

#Creats a random module for use
import random
#Creats a random number and stores it as a variable
RandomNumber = random.randint(1, 100)


#Gets user to input a number and then stores that number as a variable
Guess = int(input("I am thinking of a number between 1 and 100. Make a guess: "))

while Guess > 100 or Guess < 1:
print("Don't be that guy! Thats not a valid guess")
Guess = int(input("Make another guess: "))

if Guess > RandomNumber:
print("Your guess is to big bozo!")
Guess = int(input("Try again"))
elif Guess < RandomNumber :
print("Its gonna be bigger than that")
Guess = int(input("Try again:"))
else :
print("Your cleverer than you look.Thats the number I was thinking of")

Thats my code for my guessing game, but I don't know how to make the guesser have more than one guess, so the game ends when they guesss the right number? can any one help???

Profile icon

Hello, please put your code in a repl, it's easier to debug there.