Alan and Steven,

Many thanks for both your help.

*Alan*
Although I understand that the method you described is the most efficient
way of guessing a random number, I was trying so that my program would have
the computer making repeated random guesses between a decreasing range of
its last two closest guesses. That is if the user chooses 55 as the number
and the computers first guess is 98 then its next guess would be between 1
and 97, if the 2nd guess was 23 the 3rd guess would between 24 and 97, if
the 4th guess was 50 then the 5th guess would be between 51 and 98, the 6th
guess being 60 then the 7th guess would be between 51 and 59, etc etc. I
was hoping that this may mean the computer won't get it every time in 10
guesses and be a bit more as a human might guess (at least the first few
times they played before they cottoned on to the method you described).

And thank you for the bug spot!

Also yes there is a typo in that I didn't mean to have have that
random.randrange in there ( was meant to be a randint)

*Steven*
Thank you for the info re highlighting!

Yes I think your example below describes what I was trying to do,

>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


But (just to clarify) as I put the simple example you gave through the code
you supplied I get a little confused as it doesn't seem to save the
closest? For example this is how i worked it:

if (25 - 50) < (9999 - 50)
  closest = 25

if (60-50) < (25 - 50)
  closest = 25?

As you stated for yor example a guess of 60 would be closer than the guess
of 25.  As I said I really am a beginner and I apologise if my questions
are frustrating but I do appreciate any help offered.

Thanks again.

Kevin





On Thu, May 8, 2014 at 2:02 PM, <tutor-requ...@python.org> wrote:

> Send Tutor mailing list submissions to
>         tutor@python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
>         tutor-requ...@python.org
>
> You can reach the person managing the list at
>         tutor-ow...@python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>    1. Re: Final review (Steven D'Aprano)
>    2. Re: (no subject) (Alan Gauld)
>    3. Re: (no subject) (Steven D'Aprano)
>    4. Re: How inefficient is this code? (Neil D. Cerutti)
>    5. Re: PyCountry currency formatting woes (Sithembewena Lloyd Dube)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Thu, 8 May 2014 23:30:49 +1000
> From: Steven D'Aprano <st...@pearwood.info>
> To: tutor@python.org
> Subject: Re: [Tutor] Final review
> Message-ID: <20140508133048.GF4273@ando>
> Content-Type: text/plain; charset=utf-8
>
> On Wed, May 07, 2014 at 08:49:11PM -0700, Scott W Dunning wrote:
> [...]
> > > >>> greeting [len(greeting)]
> > >
> > > It is trying to access the character at the position "11", where the
> > > string "Hello world" doesn't contain any value in the index "11" and
> > > the maximum index is 10. So it throws the following error.
> >
> > I think this is where I am getting confused.  I guess I don?t
> > understand why/how it?s trying to access the character at the index
> > 11?
>
> The value of greeting is "Hello world". So let's write it out, showing
> the index of each character. Remember that Python starts counting from
> zero, not one:
>
> Index 0: H
> Index 1: e
> Index 2: l
> Index 3: l
> Index 4: o
> Index 5: space
> Index 6: w
> Index 7: o
> Index 8: r
> Index 9: l
> Index 10: d
>
> So the indexes start from 0, and go up to 10. How many characters are
> there? Count them, 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 <alan.ga...@btinternet.com>
> To: tutor@python.org
> Subject: Re: [Tutor] (no subject)
> Message-ID: <lkg1r2$6b1$1...@ger.gmane.org>
> 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 <st...@pearwood.info>
> 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 +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
>
>
> ------------------------------
>
> Message: 4
> Date: Thu, 08 May 2014 09:59:05 -0400
> From: "Neil D. Cerutti" <ne...@norwich.edu>
> To: tutor@python.org
> Subject: Re: [Tutor] How inefficient is this code?
> Message-ID: <lkg2j9$jbr$1...@ger.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 5/7/2014 8:39 PM, C Smith wrote:
> > I see. Thanks that is exactly what I was looking for. Would your
> > example be an instance of an object-oriented approach,
> > compartmentalizing things into functions?
>
> One of the fun parts of Project Euler is using mathematical analysis and
> other tricks to improve your algorithm.
>
> You have a nice, general Fibonacci number generator, but you can use a
> less general function to increase efficiency [1]. In this case, it
> doesn't matter--your initial solution was fast enough--but you'll soon
> be solving problems where the most general solution fails to be fast
> enough.
>
> [1] Your code is calculating about twice as many Fibonacci numbers as it
> needs to. See if you can create a way to calculate Fib(n+2) from Fib(n).
>
> --
> Neil Cerutti
>
>
>
> ------------------------------
>
> Message: 5
> Date: Thu, 8 May 2014 16:02:07 +0200
> From: Sithembewena Lloyd Dube <zebr...@gmail.com>
> To: Python <tutor@python.org>, django-us...@googlegroups.com
> Subject: Re: [Tutor] PyCountry currency formatting woes
> Message-ID:
>         <
> cah-sncdmujpsey7q27xifgkakjou5xbvo5_owf-dfbsrp85...@mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> Thank you all, babel works just fine. I also tried ccy, which isn't bad
> either - except that it returns non-unicode currency letters for countries
> in the Eurozone.
>
>
> On Mon, May 5, 2014 at 10:10 AM, Peter Otten <__pete...@web.de> wrote:
>
> > Sithembewena Lloyd Dube wrote:
> >
> > > Thanks, i was actually getting the error information to update the
> post.
> > > Apoligies to waste your time posting here - I could not find an
> > > appropriate PyCountry discussion list and my next best bet seemed to
> be a
> > > Python users' list.
> > >
> > > For those who care to look, the error is as follows (a concise example
> > > from an interactive shell:
> > >
> > > import pycountry
> > > country = pycountry.countries.get(alpha2='DE')
> > > currency = pycountry.currencies.get(numeric=country.numeric)
> > > Traceback (most recent call last):
> > > File "", line 1, in
> > > File "/usr/lib/pymodules/python2.6/pycountry/db.py", line 83, in get
> > > return self.indices[field][value]
> > > KeyError: '276'
> > >
> > > The obvious issue here is that the pycountry.countries collection does
> > not
> > > contain a currency with a numeric of 276 (Germany's numeric) - yet it
> > does
> > > contain the Euro. Any ideas as to what the way around this may be?
> >
> > It looks like the development version of babel
> >
> >
> >
> http://babel.pocoo.org/docs/api/numbers/#babel.numbers.get_territory_currencies
> >
> > can do what you want:
> >
> > $ LANG=en_US.UTF-8 python
> > Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
> > [GCC 4.8.1] on linux2
> > Type "help", "copyright", "credits" or "license" for more information.
> > >>> import babel.numbers as bn
> > >>> bn.get_territory_currencies("DE")
> > ['EUR']
> > >>> print bn.format_currency(1.234, "EUR")
> > ?1.23
> > >>> print bn.format_currency(1.234, "EUR", locale="DE")
> > 1,23 ?
> > >>> import babel
> > >>> babel.__version__
> > '2.0-dev'
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor@python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
> >
>
>
>
> --
> Regards,
> Sithu Lloyd Dube
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/tutor/attachments/20140508/4c054982/attachment.html
> >
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> https://mail.python.org/mailman/listinfo/tutor
>
>
> ------------------------------
>
> End of Tutor Digest, Vol 123, Issue 27
> **************************************
>
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to