6677

Computer Guessing Function

May 28th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. import random
  2. #Written Under Request
  3. #Python 3.1
  4. def compguess(usernum):
  5.     toohigh = []
  6.     toolow = []
  7.     correct = False
  8.     low = 0
  9.     high = 0
  10.     loopnum = 0
  11.     while correct == False:
  12.         loopnum = loopnum + 1
  13.         if loopnum == 1:
  14.             guess = random.randrange(1,1001)
  15.         else:
  16.             guess = random.randrange(low,high)
  17.         print('The Computer Has Guessed:',guess)
  18.         if guess > usernum:
  19.             print('The Computers Guess Was Too High')
  20.             guessedyet = False
  21.             toohigh.append(guess)
  22.             toohigh.sort()
  23.             high = toohigh[0]
  24.         if guess < usernum:
  25.             guessedyet = False
  26.             print('The Computers Guess Was Too Low')
  27.             toolow.append(guess)
  28.             toolow.sort()
  29.             toolow.reverse()
  30.             low = toolow[0]
  31.         if guess == usernum:
  32.             guessedyet = True
  33.             print('The Computers Guess Was Correct')
  34.             correct = True
  35.         if guessedyet == False:
  36.             print('Do You Want The Computer To Guess Again')
  37.             valid = False
  38.             while valid == False:
  39.                 print('Enter "0" For No or "1" For Yes')
  40.                 option = input(': ')
  41.                 if option in ['1','0']:
  42.                     valid = True
  43.                 else:
  44.                     print('Invalid Input')
  45.             if option == '1':
  46.                 correct = True
  47.  
  48. #Place Any Integer Or Variable Containing Integer In The Argument Field
  49. compguess(78)
Advertisement
Add Comment
Please, Sign In to add comment