and you get 11. Which makes sense: one character per
> index, there are at least ten indexes (1 through 10), plus one extra
> (index 0) makes 11. So the length of the string is 11, but the highest
> index is 10.
>
> So greeting[0] gives "H", greeting[1] gives "e", greeting[2] gives "l",
> and so on, until you get to greeting[10] which gives "d", and
> greeting[len(greeting)] => greeting[11] which is an error.
>
>
> --
> Steven
>
>
> --
>
> Message: 2
> Date: Thu, 08 May 2014 14:46:10 +0100
> From: Alan Gauld
> To: tutor@python.org
> Subject: Re: [Tutor] (no subject)
> Message-ID:
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 08/05/14 11:00, Kevin Johnson wrote:
> > user picks a number between 1 and 100 and the computer has to guess,
>
> Hmm, usually its the other way around, the computer picks a number and
> the user guesses it. Interesting twist!
>
> > The highlighted bit is where I think I'm going wrong but I just can't
> > think how to make the computer remember the previously closest highest
> > and lowest guesses,
>
> I'm not sure why you think you need to do that?
>
> But simply you create a new variable called last_guess
> or somesuch and store computer_guess there before you
> change it.
>
> But the biggest problem is that the computer is using random values
> between 1 and the current guess(+/-1). That's a very inefficient
> way to get to the place you want.
>
> A more efficient technique is something called a binary chop:
>
> You know the users number is between 1 and 100 so you start
> in the middle at 50.
>
> If its too high then the users guess is between 1 and 49
> so you pick the new middle - say 25
>
> If its too high the target is between 1 and 24m, so you pick
> the middle -> 12
>
> and so on.
>
> Similarly if the first guess is too low you know the user is
> between 51 and 100 so you pick the middle - 75
>
> If its too high you pick midway between 75 and 100 and so on.
>
> That should pretty much always get the computer to the
> right answer within its 10 guesses.
>
> The only values you need to keep track of are the lower
> and upper bounds - starting at 1 & 100 respectively.
>
> > user_number = int(input("Pick a number between 1 and 100: "))
> > computer_guess = random.randint(1, 100)
> > guesses = 1
> >
> > while computer_guess != user_number:
> ...
>
> > if computer_guess > user_number:
> > computer_guess = random.randrange(1, (computer_guess-1))
> > else:
> > computer_guess = random.randint((computer_guess + 1), 100)
> > guesses += 1
> > if computer_guess == user_number:
> > print("Well done you guessed the number was", user_number)
> > print("It took you", guesses, "tries")
>
> There is also a bug in the above code: in the case that the
> computer guesses the correct number the else clause causes the
> computer to generate a new guess.
>
> So the computer can never win on the first guess!!
>
> The else line should really be:
>
> elif computer_guess < user_number:
>
> Also why use randint() on one line but randrange() on the other?
> They are subtly different and only one is correct in this situation.
> I'll let you figure out which! :-)
>
> And if you use binary chop you won't need either!
>
>
> HTH
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
>
>
>
> --
>
> Message: 3
> Date: Thu, 8 May 2014 23:55:31 +1000
> From: Steven D'Aprano
> To: tutor@python.org
> Subject: Re: [Tutor] (no subject)
> Message-ID: <20140508135531.GG4273@ando>
> Content-Type: text/plain; charset=us-ascii
>
> Hello Kevin, and welcome!
>
> My responses are below, interleaved between yours.
>
> On Thu, May 08, 2014 at 10:00:11AM +, 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