''' Purpose: play guess a number ''' # specify max number n = 100 # start the game print( 'Think of a number from 0 to', n ) # let's try at a portion of a number # and then figure out if the number is greater than or less than that portion # and if it is, we take another portion # etc. etc. until we find the number # this is called a binary search! lowest_possible_value = 0 highest_possible_value = n # how long do we wanna keep going? (hint: a great question to ask yourself when making a certain kind of loop) while ( highest_possible_value - lowest_possible_value > 2 ): middle = ( highest_possible_value + lowest_possible_value )//2 # you don't need the == True, but if it helps, do it! # need to keep guessing prompt = "Is your number lower than " + str( middle ) + "? " reply = input( prompt ) if ( reply == "yes" ): highest_possible_value = middle # highest_possible_value = highest_possible_value // 2 # this got us in an endless loop because we weren't actually portioning the range else: lowest_possible_value = middle # print(lowest_possible_value, highest_possible_value) # integer division will get us stuck, hence this series of lines to really nail down the number prompt = "Is your number lower than " + str( lowest_possible_value ) + "? " reply = input( prompt ) if ( reply == "yes" ): print( "Your number is", highest_possible_value ) else: print( "Your number is", lowest_possible_value ) # guessing Emily's number: 8? nope # 9? nope # 7? nope # 10? nope # 11? 12? nope # 50000? # is it greater than 50000? - no # is it bigger than 10000? - no # is it bigger than 5000? - nope # bigger than 100? - nope # bigger than 80? - yup # bigger than 90? - yes # bigger than 95? - nope # it was 92! # notice how guessing got easier once we realized we could ask "is it less/greater than?"