''' Purpose: play guess a number ''' # specify max number n = 100000 # start the game print( 'Think of a number from 0 to', n ) lowest_possibility = 0 greatest_possibility = n still_guessing = True # Boolean for our while loop ( while loop will run while # this variable is True! while( still_guessing == True ): # This loop will keep guessing # until the Boolean is set to False! # stil_guessing == True is the logical expression # we're testing in the while loop! # If it is, we keep running the loop. # If it's not, we exit the loop. middle = ( lowest_possibility + greatest_possibility ) // 2 prompt = "Is your number equal to " + str( middle ) + "? " reply = input( prompt ) if ( reply == "yes" ): print( "Found it!" ) still_guessing = False # Change boolean so that we stop guessing # (so that while loop stops) # IMPORTANT: Update the Boolean (set it to false) when you # want to be done with the while loop! # In the guessing game, we want to assign still_guessing to False # when we find the number because then we're done and we don't want # the loop to run again! else: # The Boolean is still true so keep running the while loop! prompt = "Is your number greater than " + str( middle ) + "? " reply = input( prompt ) if ( reply == "yes" ): # If it's greater than the middle lowest_possibility = middle + 1 # Update the range else: # If it's less than the middle greatest_possibility = middle - 1 # Update the range print( lowest_possibility, "...", greatest_possibility ) # Show us our new range and keep running the loop (go back to # start of while loop until we find it) # We run the loop again with the new updated range (lowest_possibility # and greatest_possibility and middle are updated now)