[Chicago] How could I make this better?

kirby urner kirby.urner at gmail.com
Sun Mar 6 00:28:10 CET 2011


Is you goal to share the source code with your kids?

Dunno how young, but you might wanna make the if / elif / else stuff less
deep.  Just ifs, without elifs or elses might be cool.

I'd probably make it a while True loop (with break conditions).  A blank
guess might mean "I give up" (nice to have an escape route).

Chances of guessing the number in one or two guesses is about nil.

I'd think the proposer of this puzzle wan't very smart if I had to play such
a game.  :)

Note that in Python 3, input returns a string, not an integer (so convert --
but you can catch a blank first, as distinct from 0).

If your goal is to teach Python to your kids, I'd recommend Python 3.

Kirby

On Sat, Mar 5, 2011 at 4:34 PM, Malcolm Newsome
<malcolm.newsome at gmail.com>wrote:

> 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.
>
>
>
>
>
>
>
> # guess.py
>
> # a simple number guessing game
>
>
>
> import random
>
>
>
> 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 0 - 100.  Can you guess what it
> is?"
>
>     print
>
>
>
>     guess = input("Type a number between 0 - 100: ")
>
>
>
>     random_num = random.randrange(0,100,1)
>
>
>
>     if guess == random_num:
>
>         print "You got it! I guess you are smarter than me!"
>
>
>
>     elif guess < random_num:
>
>             # user gets second chance if number is too low
>
>             guess_iflow = input("You were too low. Type another number: ")
>
>
>
>             if guess_iflow == random_num:
>
>                 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: ", random_num
>
>
>
>     elif guess > random_num:
>
>             # user gets second chance if number is too high
>
>             guess_ifhigh = input("You were too high. Type another number:
> ")
>
>
>
>             if guess_ifhigh == random_num:
>
>                 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: ", random_num
>
>
>
>     else:
>
>         print "Nope! I'm smarter than you!"
>
>         print "I was thinking of the number: ", random_num
>
>
>
> main()
>
>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20110305/9958f997/attachment.html>


More information about the Chicago mailing list