# [Chicago] How could I make this better?

Julie Bell jmwebstuff at yahoo.com
Sun Mar 6 01:38:06 CET 2011

```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

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> 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

-----Inline Attachment Follows-----

_______________________________________________
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/68d47acb/attachment-0001.html>
```