Re: Python: Help With Beginner Stuff

  • From: "R Dinger" <rrdinger@xxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Fri, 15 Oct 2010 12:01:42 -0700

Hello again Jim,

Instead of picking a random number between your current lower and upper limits, 
the more usual algorithm is to select the value mid way between the lower and 
upper limits.

Or do you have a different approach in mind?

Richard
  ----- Original Message ----- 
  From: Homme, James 
  To: programmingblind@xxxxxxxxxxxxx 
  Sent: Friday, October 15, 2010 11:19 AM
  Subject: Python: Help With Beginner Stuff


  Hi,

  I know more than what I'm showing in this program, but I'm taking my learning 
back so as not to miss anything crucial. The book I'm reading is in the Jamal 
collection of Python docs. It's called "Python Programming for the absolute 
beginner." I'm doing the Chapter 3 assignment. The assignment was to write 
pseudocode for a program that would simulate the computer guessing a number 
from 1 to 100. I wrote the pseudocode. That made me confident enough to take a 
stab at writing the program. What follows is my program and one set of output 
from it. My question is this. It seems like the way I have worked it out, the 
computer is always guessing 10 or 11 times to get the answer. How can I make 
the simulation a little more realistic?

   

  === begin program ===

   

  # comp_guess.py

  # By Jim Homme

  # October 15, 2010

  # Simulate the computer guessing a number between 1 and 100 until it guesses 
correctly.

   

  import random

   

  lower = 1

  upper = 100

  answer = int(raw_input("Give me a number between 1 and 100 for the computer 
to guess: "))

  print "\nThe answer is %d." % (answer)

  comp_guess = random.randint(lower, upper) # The computer makes a guess

  guesses = 1

  print "For guess %d, the computer guessed %d." % (guesses, comp_guess)

  while comp_guess != answer:

    if comp_guess < answer:

      print "The computer guessed too low."

      lower  = comp_guess +1 # So it won't guess the same number again.

      print "lower is now %d." % (lower)

    else:

      print "The guess is too high."

      upper = comp_guess -1 # So it won't guess the number again.

      print "upper is now %d." % (upper)

    guesses += 1

    comp_guess = random.randint(lower, upper) # Guess again.

   

    print "For guess %d, the computer guessed %d." % (guesses, comp_guess) # 
The final guess.

  print "The computer guessed the number in %d guesses" % (guesses)

  print "Can you beat that?"

  raw_input("Press Enter to quit")

   

  === End program ===

   

  === Begin output ===

   

  Give me a number between 1 and 100 for the computer to guess: 

  The answer is 44.

  For guess 1, the computer guessed 89.

  The guess is too high.

  upper is now 88.

  For guess 2, the computer guessed 35.

  The computer guessed too low.

  lower is now 36.

  For guess 3, the computer guessed 82.

  The guess is too high.

  upper is now 81.

  For guess 4, the computer guessed 37.

  The computer guessed too low.

  lower is now 38.

  For guess 5, the computer guessed 65.

  The guess is too high.

  upper is now 64.

  For guess 6, the computer guessed 52.

  The guess is too high.

  upper is now 51.

  For guess 7, the computer guessed 41.

  The computer guessed too low.

  lower is now 42.

  For guess 8, the computer guessed 48.

  The guess is too high.

  upper is now 47.

  For guess 9, the computer guessed 47.

  The guess is too high.

  upper is now 46.

  For guess 10, the computer guessed 43.

  The computer guessed too low.

  lower is now 44.

  For guess 11, the computer guessed 44.

  The computer guessed the number in 11 guesses

  Can you beat that?

  Press Enter to quit

   

  === End Output ===

   

   

   

   

   

   

  Jim Homme,

  Usability Services,

  Phone: 412-544-1810. Skype: jim.homme

  Internal recipients,  Read my accessibility blog. Discuss accessibility here. 
Accessibility Wiki: Breaking news and accessibility advice

   



------------------------------------------------------------------------------
  This e-mail and any attachments to it are confidential and are intended 
solely for use of the individual or entity to whom they are addressed. If you 
have received this e-mail in error, please notify the sender immediately and 
then delete it. If you are not the intended recipient, you must not keep, use, 
disclose, copy or distribute this e-mail without the author's prior permission. 
The views expressed in this e-mail message do not necessarily represent the 
views of Highmark Inc., its subsidiaries, or affiliates.

Other related posts: