# Finds the identity of a number # based on halfing properties # written by George Wong|28.10.11 # assuming python is installed, this # runs as a stand-alone # imports import math import random import sys import time #pauses. if v=1, will end the program after pause; if v=2, kills program def pause(v): if(v!=0): print('') if(v==1): omega=raw_input('\nPress return to end the program...') print('\nThanks for playing!\n\nSee you later, goodbye!') time.sleep(1.5) sys.exit() else: omega=raw_input('\nPress return to continue...') #gets the min and max that the user wants to use; also #returns the number of guesses it should take def getrange(): repeat=True #gets numbers for range while(repeat): #ensures that the user enters numbers too min=raw_input('Enter the minimum number for the range: ') max=raw_input('Now enter the maximum number, please: ') if(min.isdigit()): if(max.isdigit()): min=int(min) max=int(max) repeat=False if(repeat): print('\nCome on.. enter numbers this time!\n') time.sleep(1.5) if(max<min): #fixes order of number, if necessary swap=max max=min min=swap print('...reordering your number so that they make some sense...') time.sleep(1) i=(str(int(round(math.log((max-min),2)+.5)))) # determines number of guesses print('\nRange is: [' + str(min) + ',' + str(max) + ']') return min,max,i #the computer guesses! def compguess(max,min): guess=(max+min)/2 # pretty self-explanatory stuff here print('\nIs your number greater, less than, or equal to ' + str(guess) + '?') response=raw_input('Please type \'greater\', \'less\', or \'equal\': ') if(response=='greater'): min=guess+1 elif(response=='less'): max=guess-1 elif(response=='equal'): return(max,min,1) else: # catches the user inputting a nonsensical answer print('Come on! Use proper syntax...') return(max,min,-1) return(max,min,0) #asks the human for a guess, etc. def humanguess(max,min,answer,i): print('\nYou have ' + str(i) + ' guesses left.') guess=raw_input('Enter your guess: ') if(not guess.isdigit()): #catches the improper inputs print('Failure on your part: you were supposed to enter a NUMBER') return(max,min,guess,-1) guess=int(guess) if(guess<min or guess>max): #saves user from inputting answers in wrong range print('Please enter a number in the proper range!') return(max,min,guess,-1) if(guess>answer): print('The number I\'m \'thinking\' of is LESS than that.') max=guess-1 return(max,min,guess,0) elif(guess<answer): print('The number I\'m \'thinking\' of is GREATER than that.') min=guess+1 return(max,min,guess,0) elif(guess==answer): return(max,min,guess,1) return(max,min,guess,0) #returns in the event of a complete failure of code # variable definition method = 'wait' done = 0 # splash screen|I use more spaces here than are necessary, but # it keeps the code justified, so it's worth the two extra bytes print(' /================\ \n || WELCOME TO ||') print(' || GUESSER! ||\n || -------------- ||') print(' || by george wong ||\n \================/ ') # main loop while(method=='wait'): # menu starts here! print('\nWhich mode would you like to use?') print('-----------------------') print('1 - Human guesses computer\'s number') print('2 - Computer guesses human\'s number') print('-----------------------') method = raw_input('Please enter your choice or "q" to quit: ') #quitting protocol :quits without prompt if(method=='q'): pause(2) #Human guesses elif(method=='1'): print('\n***************\nOkay, you\'re guessing a number I choose.\n') min,max,i=getrange() print('You will be given ' + i + ' guesses.\n') i = int(i) answer=random.randrange(min,max) print(' OK, so here\'s how this works:\n You\'re going to enter a number and I will') print(' tell you whether my number is greater than\n yours, less than it, or equal to it. I will') print(' also tell you how many guesses you have left.\n\nReady?') while(done==0 and i>0): max,min,guess,done=humanguess(max,min,answer,i) if(done==-1): i += 1 done=0 i -= 1 if(done==1): print('\n\nCongratulations! You guessed it.\nThe number was ' + str(answer) + '.') print('Please play again!\n') done=0 else: print('\n The number was ' + str(answer) + '.') print('\nIt appears that ran out of guesses! This\nprogram is written in such a way to allow') print('you to always guess the answer within the\ngiven number ofguesses.\n\nPlease try again!\n') time.sleep(2) # gives the user time to react done=0 method='wait' #Computer guesses **with a comp-thought varient elif(method=='2' or method=='compthought'): print('\n***************\nOkay, I\'m guessing a number you choose.\n') if(method=='2'): min,max,i=getrange() elif(method=='compthought'): min=1 max=1000 i='10' print('In range [1,1000].') print('I have ' + i + ' guesses.\n') i=int(i) while(done==0): max,min,done=compguess(max,min) i -= 1 if(done==-1): i += 1 #catches the counter exception when there's a syntax error done=0 if(max<min): print('\nYou\'re mistaken. In the future,\ndon\'t lie and you won\'t have to\nrestart the program.') pause(1) if(done==1): #checks to make sure it's done and reports final value. switches back to native settings plural='es' #these three lines fix the grammar of the next-ly printed line if(i==1): plural='' print('\nYour number was ' + str((max+min)/2) +'... Woo-hoo!\nI had ' + str(i) + ' guess' + plural + ' left!\n') done=0 method='wait' time.sleep(1.2) # why not add a random sleep time? else: #safety measure print('hmm, something went wrong...\nprogram aborting...') pause(1) #Error! :redirects user to start else: print('\n\nI\'m sorry, that was not a valid input. Please try again.\n') time.sleep(1) method='wait' # I don't know how you got out here, but we'll quit anyway... pause(1)