[Chicago] How could I make this better?

Malcolm Newsome malcolm.newsome at gmail.com
Sat Mar 5 23:34:17 CET 2011


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

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20110305/784b2074/attachment.html>


More information about the Chicago mailing list