Advertisement
brilliant_moves

ComputerGuessNumber.py

Jan 19th, 2015
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. #!/usr/bin/python
  2. '''
  3. Program:    ComputerGuessNumber.py
  4. Python Version: Python 2.7
  5. Purpose:    Computer uses binary search to guess user's number in 7 tries or less.
  6. Creator:    Chris Clarke
  7. Created:    20.01.2015
  8. '''
  9.  
  10. min, max = 1, 100
  11. computer_guess = tries = number = 0
  12.  
  13. while number<min or number>max:
  14.     number = int( input( "Enter a number between "+str(min)+" and "+str(max)+": "))
  15.  
  16. while computer_guess!=number and tries<=7:
  17.     computer_guess = (min+max)/2
  18.     print computer_guess
  19.     if computer_guess<number:
  20.         min = computer_guess+1
  21.     elif computer_guess>number:
  22.         max = computer_guess
  23.     tries += 1
  24.  
  25. if tries<=7:
  26.     print "Your number, %d, was found in %d tries." % (computer_guess, tries)
  27. else:
  28.     print "Number not found in 7 tries."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement