[Chicago] pedantic guessing game demo

Malcolm Newsome malcolm.newsome at gmail.com
Sat Mar 26 22:24:33 CET 2011

*Hey All,

Here's a rewrite of the guessing game taking into account some of your
suggestions.  There's a few more things I need to figure out. But, I'm
pleased with how it has progressed so far.

All the best!


# guess.py
# a simple number guessing game

import random

def nope_message(random_num):
    return "Nope! I'm smarter than you!\nI was thinking of the number:
%d" % int(random_num)

def right_message(retried=False):
    message = "That was right! I guess you ARE smarter than me"

    if retried:
        message += "... even though it took you another try!"

    return message

def reread_input(message):
    return int(input("You were too %s. Type another number: " % message))

def retry(message, random_num):
    guess_iflow = reread_input(message)

    if guess_iflow == random_num:
        return right_message(True)
        return nope_message(random_num)


def main():
    print "Do you think you're smarter than me?"
    print "I guess we'll see!"
    print "I'm thinking of a number between 1 - 100.  Can you guess what it is?"

    random_num = random.randint(1, 100)

    guess = int(input("Type a number between 1 - 100: "))

    error = guess < 1, guess < 100

    if guess == random_num:
        print right_message()
    elif guess < random_num:# user gets second chance if number is too low
        print retry("low", random_num)
    elif guess > random_num:# user gets second chance if number is too high
        print retry("high", random_num)
        print nope_message(random_num)

if __name__ == "__main__":

Tue Mar  8 07:15:18 CET 2011*

Well, then, you still did well enough on a first attempt.

This is a good starting place to learn about more techniques, like
using methods, loops, and conditionals. Learning to use loops and
functions will probably be next for you.

Keep reading up on python, maybe using one of the nice online books or
the tutorial that comes with it. You will learn about classes,
functions, conditionals, loops and many exciting other features of our
favorite programming language.

It actually is a bit of fun to rewrite the program using Test-Driven
Development (when you get that far along).

On Mon, Mar 7, 2011 at 2:23 PM, Tim Ottinger <tottinge at gmail.com
<http://mail.python.org/mailman/listinfo/chicago>> wrote:
>* For children, I think this is a good start. Without introducing
*>* looping, and barely introducing subroutines, it's not bad for kids.
*>* I took a copy and started by running  autonose in the directory and
*>* writing unit tests. That drove me to make a lot
*>* of changes.  I ended up breaking out a game object, introducing member
*>* functions, isolating the input and output from the core game, etc.
This mirrors my thoughts when I looked at the code.  It is all written
in one large function, with no distinction between user interaction,
processing or data manipulation.  There are no tests, and no easy way
to inject testing.

I started to think about a data object that would initialize itself
and compare itself against a guess.  I considered proposing a user
interface object that would read its strings externally so it could
easily be moved to different languages.  And a process controller that
would isolate the looping constructs could be added.

But then again it's a one page function that has no plans to grow.
All the above is premature optimization and should be done as part of
refactoring processes as the code is prepped for the addition of the
next step.

Actually, If I was trying to get my kids interested, I would modify
this into a web app where they could see the HTML and start to
understand how the web is technology not magic. Being able to put in a
background image and change the look of the page is fun.  And when you
show them that if they view the page source they can find the 'secret'
number in the text of a hidden field they feel a thrill like they are
cracking a secret of the universe.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20110326/859256db/attachment.html>

More information about the Chicago mailing list