Finding Halfway Between Two Numbers Game
Halfway Between Numbers Practice and Lesson - This is for kids that have trouble after you move to independent practice. This should shore it up for them. Halfway Practice Worksheet - What number is halfway between these two? Determining the Halfway Mark Five Pack - We give you two numbers. You need to find the midway point of the two numbers.
I am very new to programming so I decided to start with Python about 4 or 5 days ago. I came across a challenge that asked for me to create a 'Guess the number' game. After completion, the 'hard challenge' was to create a guess the number game that the user creates the number and the computer (AI) guesses.
So far I have come up with this and it works, but it could be better and I'll explain.
This is what happened on my last run:
Enter a number for the computer to guess: 78
The computer takes a guess... 74
The computer takes a guess... 89
The computer takes a guess... 55
The computer takes a guess... 78
The computer guessed 78 and it was correct!
Notice that it works, however when the computer guessed 74, it then guessed a higher number to 89. The number is too high so the computer guesses a lower number, however the number chosen was 55. Is there a way that I can have the computer guess a number that is lower than 89, but higher than 74? Would this require additional variables or more complex if, elif, else statements?
Thank you Ryan Haining
I used the code from your reply and altered it slightly so the guess is always random. If you see this, let me know if this is the best way to do so.
8 Answers
what you are looking for is the classic binary search algorithm
The algorithm works by selecting a low and high limit to start with (in your case low=1 and high=100). It then checks the midpoint between them.
If the midpoint is less than number, the midpoint becomes the new lower bound. If the midpoint is higher, it becomes the new upper bound. After doing this a new midpoint is generated between the upper and lower bound.
To illustrate an example let's say you're looking for 82.
Here's a sample run
So what's happening here in each step?
low = 1
,high = 100
=>guess = 50
50 < 82 solow = 51
low = 51
,high = 100
=>guess = 75
75 < 82 solow = 76
low = 76
,high = 100
=>guess = 88
88 > 82 sohigh = 88
low = 76
,high = 88
=>guess = 82
82 82 and we're done.
Note that the time complexity of this is O(lg(N))
Halfway Between Two Numbers
I briefly made the game which you need with follows:
This is a bit rusty, but you could improve it by actually narrowing down the choices so the computer has a greater chance of guessing the right number. But where would the fun in that be? Notice how in my code, if the computer guesses a number which is greater than than the number the user has inputed, it will replace 100 from the randint function with that number. So if it guesses 70 and its too high, it won't choose a number greater than 70 after that. I hope this helps, just ask if you need any more info. And tell me if it's slightly glitchy
What I did for the same challenge was:
1) Define a variable that records the max value input by guessing computer.Ex:
2) Define another variable with the lowest guessed value.Ex.
3) Added in the 'if computer_guess > secret_number' clause the following code (I added -1 so that the computer wouldn't try to guess the already previously tried number):
4) Added the following code in the 'if computer_guess < secret_number':
Worth noting is the fact that I set my while loop to loop until another variable 'guess_status' changes into a value 1 (the default I set to 0). This way I actually saw the result when the while loop finished.
You only need two new variables to keep track of the low and high limits :
Miklos AubertMiklos AubertIf you use the stuff in the chapter (guessing this is from the Dawson book) you can do it like this.
Prune