RE: Python Challenge #2: Guessing The Number 01-11-2016, 01:36 AM
#10
Pretty sure that the best way for the computer to guess in the advanced version is by a sort of binary chop algorithm, which is sort of what I've done.
Code:
def aiguess(remaining):
mid = int((remaining[0]+remaining[1])/2)
return mid
num = int(input("Set the number (Between 1 and 100): "))
while num > 100 or num < 0:
num = int(input("Set the number (Between 1 and 100): "))
guesses = 0
remaining = [0,100]
done = False
while done != True:
guess = aiguess(remaining)
print("The computer guessed: ",guess)
if guess < num:
print("You tell the computer that it is higher than its guess.")
remaining[0] = guess + 1
guesses += 1
elif guess > num:
print("You tell the computer that it is lower than its guess.")
remaining[1] = guess - 1
guesses += 1
else:
guesses += 1
done = True
print("The computer guessed your number in ",guesses," guess(es)!")
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)