Session 18

PP 23, 24, 25

Exercise 23 and 24 weren’t too difficult, so I don’t have too much to say about them, but wow did I have trouble with exercise 25! It took me three tries over three days to finally to solve it completely and correctly.

The first approach I came up with used a very complicated set of nested if statements to look at the previous one, two or three guesses to formulate the next guess. The logic became way to complicated for me to follow and while it could guess correctly some of the time, it also had a tendency to run itself in an infinite loop, which I couldn’t fix. Also, I know that using a bunch of nested if statements is bad practice, so I wanted to come up with a better approach.

The second solution involved using the “high” and “low” guess lists that I used in the final solution, but I ran into syntax error involving the lists that I couldn’t fix, even after troubleshooting it extensively.

Finally, I threw out solution two (which wasn’t really a solution, since it didn’t work) and re-wrote it from scratch. What I ended up with was much more concise than if I had kept the original structure, and… it worked!

You can see that solution below. Note that I had the computer start with a random guess, to make the problem a bit more complicated to solve. I also used print statements all over to see that it was working correctly:

If you have any comments on the solution, please let me know!