Hello Kevin, and welcome! My responses are below, interleaved between yours.
On Thu, May 08, 2014 at 10:00:11AM +0000, Kevin Johnson wrote: > Hi, > > Total beginner to python and am working my way through Michael Dawsons > 'Absolute beginner' book. Just got stuck on the last bit of the challenges > from chapter 3. Essentially need to create a game where the user picks a > number between 1 and 100 and the computer has to guess, program should > indicate to the computer if the guess need to be higher or lower, it should > also count the number of attempts and call a halt to the game if a set > number of attempts is reached. > > The highlighted bit is where I think I'm going wrong Many programmers -- myself included -- operate exclusively with "plain text", no formatting. I won't go into all the reasons, but let's just assume we have a good reason for it. Unfortunately that means that many of us can't see your highlighting. If you want to maximize the number of tutors here who can answer your questions, you may like to find a way to comment your code other than coloured highlighting. A good way is to insert a comment, like # This is the section I'm having problems with. [code goes here] # This is the end of the section. Or similar. > but I just can't think > how to make the computer remember the previously closest highest and lowest > guesses, Any time you want the computer to remember something, the most obvious way is to give it a variable. I'm not quite sure what you mean by "previously closest highest and lowest guesses", but I'm going to take a stab at it. You want the computer to compare the current guess to your number, and if the guess is *closer* to the previous closest guess, remember it. E.g. suppose you pick the number 50, and the computer guesses 25. Since this is the first guess, it's automatically the closest. Then the computer guesses 60. Since 60-50 = 10 is smaller than 50-25 = 25, 60 is the closest. Then the computer guesses 30. Since 50-30 = 20 is greater than 10, 60 remains the closest. Am I on the right track? Assuming I am, how might I program this? The first thing is to have a variable that holds the closest value: closest = ... what? Here's a trick. Since we know that the guesses are always between 1 and 100, if we set the first "closest" to something greater than 100, the first guess will always count as closer! So: closest = 9999 Then, as you check each guess: if abs(computer_guess - user_number) < abs(closest - user_number): # this guess is closer than the previous closest closest = computer_guess With a little thought you should be able to extend this idea to keeping two variables, the closest_high number and the closest_low number. If the computer's guess is too low, you check and update closest_low; if the guess is too high, you check and update closest_high. Regards, -- Steven _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor