Is you goal to share the source code with your kids?<div><br></div><div>Dunno how young, but you might wanna make the if / elif / else stuff less deep.  Just ifs, without elifs or elses might be cool.</div><div><br></div><div>
I&#39;d probably make it a while True loop (with break conditions).  A blank guess might mean &quot;I give up&quot; (nice to have an escape route).</div><div><br></div><div>Chances of guessing the number in one or two guesses is about nil.</div>
<div><br></div><div>I&#39;d think the proposer of this puzzle wan&#39;t very smart if I had to play such a game.  :)</div><div><br></div><div>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).</div>
<div><br></div><div>If your goal is to teach Python to your kids, I&#39;d recommend Python 3.</div><div><br></div><div>Kirby</div><div><br><div class="gmail_quote">On Sat, Mar 5, 2011 at 4:34 PM, Malcolm Newsome <span dir="ltr">&lt;<a href="mailto:malcolm.newsome@gmail.com">malcolm.newsome@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div lang="EN-US" link="blue" vlink="purple"><div><p class="MsoNormal">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.</p>
<p class="MsoNormal"> </p><p class="MsoNormal"> </p><p class="MsoNormal"> </p><p class="MsoNormal"># guess.py</p><p class="MsoNormal"># a simple number guessing game</p><p class="MsoNormal"> </p><p class="MsoNormal">import random</p>
<p class="MsoNormal"> </p><p class="MsoNormal">def main():</p><p class="MsoNormal"> </p><p class="MsoNormal">    print &quot;Do you think you&#39;re smarter than me?&quot;</p><p class="MsoNormal">    print &quot;I guess we&#39;ll see!&quot;</p>
<p class="MsoNormal">    print &quot;I&#39;m thinking of a number between 0 - 100.  Can you guess what it is?&quot;</p><p class="MsoNormal">    print</p><p class="MsoNormal"> </p><p class="MsoNormal">    guess = input(&quot;Type a number between 0 - 100: &quot;)</p>
<p class="MsoNormal"> </p><p class="MsoNormal">    random_num = random.randrange(0,100,1)</p><p class="MsoNormal"> </p><p class="MsoNormal">    if guess == random_num:</p><p class="MsoNormal">        print &quot;You got it! I guess you are smarter than me!&quot;</p>
<p class="MsoNormal"> </p><p class="MsoNormal">    elif guess &lt; random_num:</p><p class="MsoNormal">            # user gets second chance if number is too low</p><p class="MsoNormal">            guess_iflow = input(&quot;You were too low. Type another number: &quot;)</p>
<p class="MsoNormal"> </p><p class="MsoNormal">            if guess_iflow == random_num:</p><p class="MsoNormal">                print &quot;&quot;&quot;That was right! I guess you&#39;re smarter than me...</p><p class="MsoNormal">
                even though it took you another try!&quot;&quot;&quot;</p><p class="MsoNormal"> </p><p class="MsoNormal">            else:</p><p class="MsoNormal">                print &quot;Nope! I&#39;m smarter than you!&quot;</p>
<p class="MsoNormal">                print &quot;I was thinking of the number: &quot;, random_num</p><p class="MsoNormal"> </p><p class="MsoNormal">    elif guess &gt; random_num:</p><p class="MsoNormal">            # user gets second chance if number is too high</p>
<p class="MsoNormal">            guess_ifhigh = input(&quot;You were too high. Type another number: &quot;)</p><p class="MsoNormal"> </p><p class="MsoNormal">            if guess_ifhigh == random_num:</p><p class="MsoNormal">
                print &quot;&quot;&quot;That was right! I guess you&#39;re smarter than me...</p><p class="MsoNormal">                even though it took you another try!&quot;&quot;&quot;</p><p class="MsoNormal"> </p><p class="MsoNormal">
            else:</p><p class="MsoNormal">                print &quot;Nope! I&#39;m smarter than you!&quot;</p><p class="MsoNormal">                print &quot;I was thinking of the number: &quot;, random_num</p><p class="MsoNormal">
 </p><p class="MsoNormal">    else:</p><p class="MsoNormal">        print &quot;Nope! I&#39;m smarter than you!&quot;</p><p class="MsoNormal">        print &quot;I was thinking of the number: &quot;, random_num</p><p class="MsoNormal">
 </p><p class="MsoNormal">main()</p><p class="MsoNormal"> </p></div></div><br>_______________________________________________<br>
Chicago mailing list<br>
<a href="mailto:Chicago@python.org">Chicago@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/chicago" target="_blank">http://mail.python.org/mailman/listinfo/chicago</a><br>
<br></blockquote></div><br></div>