Guessing algorithm does not seem to work, guessing number by Python

前端 未结 3 1607
长情又很酷
长情又很酷 2021-01-22 00:34

I am struggling with some simple algorithm which should make python guess the given number in as few guesses as possible. It seems to be running but it is extremely slow. What a

3条回答
  •  猫巷女王i
    2021-01-22 01:21

    Your logic is backwards. You want to lower the max when you guess too high and raise the min when you guess too low. Try this:

    if guess == number:
        print("The number has been found in ",total," guesses!")
        guessed = 1
    elif guess > number:
        max = guess - 1
    elif guess < number:
        min = guess + 1
    

提交回复
热议问题