title: Putting a Try and Except inside a Loop
body: <p>Hi I'm trying to figure out how to put a 'Try and Except' inside of a while loop. My brother challenged me to creating a program of the computer creating a random number (1-100) then the user to try and guess what it is. I managed to get this working but I am stuck on if the user didn't actually input a number, what I should do so that the program doesn't just stop working but just prompts the user in what they have done wrong. Below I have attached my full code. The program works, but I am not sure how to put the While loop in it. I have tried doing things like 'While guess != integer' and I've been looking up different ways on how I could do it. The best I've seen is someone just saying to put the try and except into a while loop, but it didn't tell me how I could do it. <a href="https://stackoverflow.com/questions/8069057/try-except-inside-a-loop">try-except inside a loop</a> . If possible I also wondered if there was anyway I could just call this loop at any point when the user answers, so if they do continuously mistype I don't need to do anything else. Thank you for taking your time to read this, sorry I wrote a lot </p>
  
  <pre><code>import random
  import time
  def guessMyNumber():
  print("Hello , welcome to Guess My Number")
  time.sleep(1)
  print ("The computer is thinking of a number between 1-100")
  time.sleep(2)
  print("Try to guess the number in as few attempts as possible")
  number=random.randint(1,100)
  try:
      guess=int(input("Take a guess "))
  except ValueError:
      print("You must enter a whole number")
      guess=int(input("Please take another guess, making sure that it is a whole number. Thank you. "))
   tries = 1
  while guess !=number:
      if guess&gt;number:
          print("You need to go lower")
          guess=int(input("Take a guess "))
      else:
          print("Go higher!")
          guess=int(input("Take a guess "))
      tries=tries+1
  if tries &lt; 5:
      print("Well done! You guessed the number in", tries, "tries! If you would like to play again please type 'guessMyNumber()'")
  else:
          print("You guessed the number in", tries, "tries. If you would like to play again type 'guessMyNumber()' ")
  </code></pre>
  
  <p>guessMyNumber()</p>