[Chicago] How could I make this better?

Jon Sudlow jsudlow at gmail.com
Sun Mar 6 11:31:26 CET 2011


why are printing the string using three quotes? I know three means a doc
string... just not sure.

if guess_iflow == random_num:

                print """That was right! I guess you're smarter than me...

                even though it took you another try!"""


Ok you said this program is for children. I think the flow of ifs and elifs
might be a little complex for a child.


Why not set it up like this for a first try for a child programmer?


Generate random number

Ask for a guess

if guess is too high say "to high"

if guess is too low say "to low"

if guess equeals the random number "you got it, exit"

repeat


You can eliminate that whole second flow if you just have it repeat the
process each time with a new guess input from the child.


This also enables you to guess an unlimited amount of times at the answer
each time comming closer and closer. Its kind of a cool game for a first
try.



On Sat, Mar 5, 2011 at 6:38 PM, Julie Bell <jmwebstuff at yahoo.com> wrote:

> I am not a true python programmer. But I'd put the repeating messages into
> a subroutine.
> 1. To save on code space
> 2.  To be able to change message in one place
> 3. Easier Readability of program
>
> The other thing I would do is put it into a loop the tries.
>
>
>  #simple number guessing game
>
> import random
>
> def correct():
>
>         print "You got it! I guess you are smarter than me!"
>     return
>
> def correct2():
>
>         print """That was right! I guess you're smarter than me...
>         even though it took you another try!"""
>     return
>
> def wrong(random_num):
>
>
>         print "Nope! I'm smarter than you!"
>         print "I was thinking of the number: ", random_num
>     return
>
> 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:
>     correct()
>
>
>     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:
>         correct2()
>             else:
>         wrong(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:
>         correct2()
>             else:
>         wrong(random_num)
>     else:
>     wrong(random_num)
>
> main()
>
>
> --- On *Sat, 3/5/11, kirby urner <kirby.urner at gmail.com>* wrote:
>
>
> From: kirby urner <kirby.urner at gmail.com>
> Subject: Re: [Chicago] How could I make this better?
> To: "The Chicago Python Users Group" <chicago at python.org>
> Date: Saturday, March 5, 2011, 5:28 PM
>
>
> 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<http://mc/compose?to=malcolm.newsome@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://mc/compose?to=Chicago@python.org>
> http://mail.python.org/mailman/listinfo/chicago
>
>
>
> -----Inline Attachment Follows-----
>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org <http://mc/compose?to=Chicago@python.org>
> http://mail.python.org/mailman/listinfo/chicago
>
>
>
> _______________________________________________
> 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/20110306/5fdb3529/attachment.html>


More information about the Chicago mailing list