Skip to content Skip to sidebar Skip to footer

While Loop Guessing Number Game - Python

I'm trying to make a 'guess the number between 1-10' game but the while loops seems to keep running. I want to program to let the user guess a number then display if its too high o

Solution 1:

You forgot to guess inside the loop

while guess != randNum:
    guess = int(input("Try to guess the number:"))
    if (guess > randNum):
      print"Wrong! You guessed too high"if (guess < randNum):
      print"Wrong! You guessed too low"print"You got it!"

Solution 2:

If you move the input statement into the while loop, you should be fine.

Solution 3:

Use this:

import random

defnumberGuess():
  print("I'm thinking of a number between 1 and 10")
  randNum = random.randrange(1,11) # this line generates a random numberwhile guess != randNum:
    guess = int(input("Try to guess the number:")) # ask user for a numberif (guess == randNum): 
      print"You got it!"if (guess > randNum):
      print"Wrong! You guessed too high"if (guess < randNum):
      print"Wrong! You guessed too low"

numberGuess()

Solution 4:

import random

defnumberGuess():
  randNum = random.randrange(1,11) # this line generates a random number
  guess = int(input("Try to guess the number:")) # ask user for a numberprint (randNum)
  whileTrue:
    if (guess == randNum):
        print ("You got it!")
        breakif (guess > randNum):
        print ("Wrong! You guessed too high")
        guess = int(input("Try to guess the number:"))  # ask user for a numberif (guess < randNum):
        print ("Wrong! You guessed too low")
        guess = int(input("Try to guess the number:"))  # ask user for a number

numberGuess()

Post a Comment for "While Loop Guessing Number Game - Python"