print (" This program is going to guess the number that you are thinking of. " +"Pick one number, any integer between 1 and 1000 inclusively!") number_of_questions = 1 smallest_number = 1 highest_number = 1000 guess = int((smallest_number + highest_number)/2) equal = False #means whether our guess is equal" while number_of_questions < 10: #as long as up to 9 questions are asked, do this answer = raw_input("Question" + str(number_of_questions) + ": Is your number greater than, less than, or equal to " + str(guess) + "?") if (answer == "greater" or answer == "Greater"): smallest_number = guess guess = int((smallest_number + highest_number)/2)#changes the average based on the answer elif (answer == "less" or answer == "Less"): highest_number = guess guess = int((smallest_number + highest_number)/2) #changes the average based on the answer elif (answer == "equal" or answer == "Equal"): smallest_number = guess highest_number = guess equal = True print("Your number is: " + str(guess)) print("We only needed to ask " + str(number_of_questions) + " questions! ") #once we know the guess is equal, we stop asking questions break #ends the loop else: #if the input does not match our if clauses, user enters again print("Please enter the answer exactly how they are displayed in the question." + "Enter 'greater/Greater', 'less/Less', or 'equal/Equal'. ") number_of_questions += 1 answer = raw_input("Question" + str(number_of_questions) + ": Is your number greater than, less than, or equal to" + str(guess) + "?") if (answer == "greater" or answer == "Greater"): #does the same thing as above smallest_number = guess guess = int((smallest_number + highest_number)/2)#changes the average based on the answer elif (answer == "less" or answer == "Less"): highest_number = guess guess = int((smallest_number + highest_number)/2) #changes the average based on the answer elif (answer == "equal" or answer == "Equal"): smallest_number = guess highest_number = guess equal = True print("Your number is: ", guess) print("We only needed to ask " + str(number_of_questions) + " questions! ") #once we know the guess is equal, we stop asking questions break #ends the loop number_of_questions += 1 #accumulates the number of questions if equal == False: #prints the final question answer = raw_input("Question" + str(number_of_questions) + ": Is your number equal to " + str(guess)+ "?") if (answer == "yes" or answer == "Yes"): equal = True print("Your number is " + str(guess)) number_of_questions += 1 print("We only needed to ask ", number_of_questions, "questions! ") elif (answer == "no" or answer == "No"): print("You gave contradicting information. Go sober up. ") elif (number_of_questions > 10): print("You gave contradicting information, so we cannot guess your number.")