Login Register


Legends Python Challenge #2: Guessing The Number filter_list
Author
Message
RE: Python Challenge #2: Guessing The Number #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)!")

Reply





Messages In This Thread
Python Challenge #2: Guessing The Number - by Nil - 12-16-2015, 08:04 PM
RE: Python Challenge #2: Guessing The Number - by Rick - 01-11-2016, 01:36 AM



Users browsing this thread: 1 Guest(s)