[Chicago] pedantic guessing game demo

Jeremy McMillan aphor at me.com
Mon Mar 7 18:17:33 CET 2011


I'd assume you're intending to demonstrate procedural/imperative  
programming:

Make the computer do this, then this other thing, and then yet another  
thing, which will always get the results we intend (describe elsewhere).

I also noticed your program lies. First you tell the player "I'm  
thinking of a number between 0 - 100..." Then the player guesses, and  
then the program actually "thinks of a number between 0 - 100."

While this is of little consequence here, a child who accepts this as  
correct might grow up to be a programmer that accepts all kinds of  
garbage as correct, ergo producing a lifetime of buggy foggy-headed  
programs inflicting inestimable damage possibly even for longer than  
the child will ever live. What a disservice!

So to answer your question, "efficient" depends entirely on your goal.  
Maybe your children are older or more sophisticated and you want to  
demonstrate that you've monkey patched their 'input' and  
'random.randrange' implementations to guarantee that the generator  
will never produce a number that has been recently accepted as input.  
In this (esoteric) case, I'd say your setup is very elegant and  
efficient.

I don't think this is a very efficient program for minimally  
demonstrating conditional code in Python to the broadest audience of  
children though. Maybe try this?

---
# guess.py
# Subject your friends to a simple game that is almost impossible to  
win!

def game(min = 1, max = 2, step = 1):
	print "Do you think you're smarter than me?"
	print "I guess we'll see!"
	
	secret_number = random.randrange(min, max, step)
	
	print "I'm thinking of a number between %d - %d.  Can you guess what  
it is?" % (min, max)
	
	guess = input("Type a number between 0 - 100: ")
	
	if guess == secret_number:
		print "You got it! I guess you are smarter than me!"
	else:
		if guess < secret_number:
			reason = "too low"
		else:
			reason = "too high"
		
		print "%d is %s, but you can guess again." % (guess, reason)
		second_guess = input("Type another number: ")
		
		if second_guess == secret_number:
			print """That was right! I guess you're smarter than me...
				even though it took you another try!"""
		else:
			print "Nope! I'm smarter than you!"
			print "I was thinking of the number: ", secret_number
			print "We can't go out like that! Let's try something easier?"
			game()

game(min = 1, max = 100)

---

On Mar 5, 2011, at 6:44 PM, chicago-request at python.org wrote:

> From: "Malcolm Newsome" <malcolm.newsome at gmail.com>
> To: <chicago at python.org>
> Subject: [Chicago] How could I make this better?
> Message-ID: <002e01cbdb85$778be020$66a3a060$@gmail.com>
> Content-Type: text/plain; charset="us-ascii"
>
> Hey all.  I wrote this simple program for my children today.  I'm  
> wondering
> if there was a more efficient alternative to the way I wrote it.
>
>
...
>    print "I'm thinking of a number between 0 - 100.  Can you guess  
> what it
> is?"
>    print
>    guess = input("Type a number between 0 - 100: ")
>    random_num = random.randrange(0,100,1)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20110307/cce0d4fe/attachment.html>


More information about the Chicago mailing list