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'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).</div><div><br></div><div>Chances of guessing the number in one or two guesses is about nil.</div>
<div><br></div><div>I'd think the proposer of this puzzle wan'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'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"><<a href="mailto:malcolm.newsome@gmail.com">malcolm.newsome@gmail.com</a>></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 "Do you think you're smarter than me?"</p><p class="MsoNormal"> print "I guess we'll see!"</p>
<p class="MsoNormal"> print "I'm thinking of a number between 0 - 100. Can you guess what it is?"</p><p class="MsoNormal"> print</p><p class="MsoNormal"> </p><p class="MsoNormal"> guess = input("Type a number between 0 - 100: ")</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 "You got it! I guess you are smarter than me!"</p>
<p class="MsoNormal"> </p><p class="MsoNormal"> elif guess < random_num:</p><p class="MsoNormal"> # user gets second chance if number is too low</p><p class="MsoNormal"> guess_iflow = input("You were too low. Type another number: ")</p>
<p class="MsoNormal"> </p><p class="MsoNormal"> if guess_iflow == random_num:</p><p class="MsoNormal"> print """That was right! I guess you're smarter than me...</p><p class="MsoNormal">
even though it took you another try!"""</p><p class="MsoNormal"> </p><p class="MsoNormal"> else:</p><p class="MsoNormal"> print "Nope! I'm smarter than you!"</p>
<p class="MsoNormal"> print "I was thinking of the number: ", random_num</p><p class="MsoNormal"> </p><p class="MsoNormal"> elif guess > random_num:</p><p class="MsoNormal"> # user gets second chance if number is too high</p>
<p class="MsoNormal"> guess_ifhigh = input("You were too high. Type another number: ")</p><p class="MsoNormal"> </p><p class="MsoNormal"> if guess_ifhigh == random_num:</p><p class="MsoNormal">
print """That was right! I guess you're smarter than me...</p><p class="MsoNormal"> even though it took you another try!"""</p><p class="MsoNormal"> </p><p class="MsoNormal">
else:</p><p class="MsoNormal"> print "Nope! I'm smarter than you!"</p><p class="MsoNormal"> print "I was thinking of the number: ", random_num</p><p class="MsoNormal">
</p><p class="MsoNormal"> else:</p><p class="MsoNormal"> print "Nope! I'm smarter than you!"</p><p class="MsoNormal"> print "I was thinking of the number: ", 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>