title: Guess the number game optimization (user creates number, computer guesses)
body: <p>I am very new to programming so I decided to start with Python about 4 or 5 days ago.  I came across a challenge that asked for me to create a "Guess the number" game.  After completion, the "hard challenge" was to create a guess the number game that the user creates the number and the computer (AI) guesses.  </p>
  
  <p>So far I have come up with this and it works, but it could be better and I'll explain.</p>
  
  <pre><code>from random import randint
  
  print ("In this program you will enter a number between 1 - 100."
         "\nAfter the computer will try to guess your number!")
  
  number = 0
  
  while number &lt; 1 or number &gt;100:
      number = int(input("\n\nEnter a number for the computer to guess: "))
      if number &gt; 100:
          print ("Number must be lower than or equal to 100!")
      if number &lt; 1:
          print ("Number must be greater than or equal to 1!")
  
  guess = randint(1, 100)
  
  print ("The computer takes a guess...", guess)
  
  while guess != number:
      if guess &gt; number:
          guess -= 1
          guess = randint(1, guess)
      else:
          guess += 1
          guess = randint(guess, 100)
      print ("The computer takes a guess...", guess)
  
  print ("The computer guessed", guess, "and it was correct!")
  </code></pre>
  
  <p>This is what happened on my last run:</p>
  
  <p>Enter a number for the computer to guess: 78</p>
  
  <p>The computer takes a guess... 74</p>
  
  <p>The computer takes a guess... 89</p>
  
  <p>The computer takes a guess... 55</p>
  
  <p>The computer takes a guess... 78</p>
  
  <p>The computer guessed 78 and it was correct!</p>
  
  <p>Notice that it works, however when the computer guessed 74, it then guessed a higher number to 89.  The number is too high so the computer guesses a lower number, however the number chosen was 55.  Is there a way that I can have the computer guess a number that is lower than 89, but higher than 74?  Would this require additional variables or more complex if, elif, else statements?</p>
  
  <p><strong>Thank you Ryan Haining</strong></p>
  
  <p>I used the code from your reply and altered it slightly so the guess is always random.  If you see this, let me know if this is the best way to do so.</p>
  
  <pre><code>from random import randint
  
  def computer_guess(num):
      low = 1
      high = 100
      # This will make the computer's first guess random
      guess = randint(1,100)
      while guess != num:
          print("The computer takes a guess...", guess)
          if guess &gt; num:
              high = guess
          elif guess &lt; num:
              low = guess + 1
          # having the next guess be after the elif statement
          # will allow for the random guess to take place
          # instead of the first guess being 50 each time
          # or whatever the outcome of your low+high division
          guess = (low+high)//2    
  
      print("The computer guessed", guess, "and it was correct!")
  
  
  def main():
      num = int(input("Enter a number: "))
      if num &lt; 1 or num &gt; 100:
          print("Must be in range [1, 100]")
      else:
          computer_guess(num)
  
  if __name__ == '__main__':
      main()
  </code></pre>