[Tutor] (no subject)

2014-05-08 Thread Kevin Johnson
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 but I just can't think
how to make the computer remember the previously closest highest and lowest
guesses, not sure if you have all read Micheal Dawsons book but I've not
covered a whole lot by chapter 3 and so any hints/solutions need to be
pretty basic as I don't want to get ahead of myself.

Thanks,
Kevin

#numbers game again but...
#user picks number between 1 and 100
#and computer guesses

import random

user_number = int(input(Pick a number between 1 and 100: ))
computer_guess = random.randint(1, 100)
guesses = 1

while computer_guess != user_number:
print(The computers guess is, computer_guess)
if computer_guess  user_number:
print(Lower...)
else:
print(Higher...)
if guesses == 10:
print(You lose computer!)
break
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)

input(Press the enter key to exit.)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 123, Issue 27

2014-05-08 Thread Kevin Johnson
 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 +, 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