[Tutor] Guessing Game Program

Donald Bedsole drbedsole at gmail.com
Fri Mar 25 04:54:55 CET 2011


Hi folks,

This is a little program I've written to bring together some things
I've been trying to learn (not an attempt, of course, to make an
interesting game)..  I've been working my way through a beginner's
tutorial, and except for a very basic program I wrote in C++ one time,
I think this is the first program I've ever done from scratch.  Of
course, I've incorporated ideas I've learned from folks on the
Internet, my tutorial, and this list.

I"m sure I've broken some rules along the way, but it does work
without any errors (that I have found).

So, how could I improve it?  Is it readable to you, or a mess?

Thanks for your time,

Don


#Author D.Bedsole
#drbedsole at gmail.com
#3/24/10
#License: Public Domain


import random
from sys import exit

#A guessing game where user must guess a number between 1 and 10



def start_up():
    print "Hello, and welcome to the Guessing Game. Here are the rules:\n"
    print "Try to guess the correct number.  It is between 1 and 10."
    print "You have ten chances."
    print "To quit the game just type 'quit' (w/o the quotes) at the prompt.\n"
    print "Here we go!\n"



def ran_num():
    the_number = random.randint(1,10)
    return the_number

def guess_loop(the_number):
        start_number = 0
        while start_number < 10:
            print "Please guess a number between 1-10. Enter your
guess at the prompt."
            guess = (raw_input("> "))	
	    if "exit" in guess: #Give user a chance to exit w/o finishing game
                print "Thanks for playing. Goodbye!"
	        exit()
	
	
	    if  guess.isdigit(): #validate input
	        guess = int(guess)
	    else:
	        print "You didn't enter a number."
	        continue #Return to top of the loop so user can enter number	
	
	
	
	    if guess > the_number:
	        print "Sorry. You guessed too high."
	        start_number += 1
	        attempt_counter(start_number, the_number)#warn user of remaining tries

	    elif guess < the_number:
	        print "Sorry. That was too low.\n"
                start_number += 1
	        attempt_counter(start_number, the_number)#warn user of remaining tries
	    else:
	        print "Congratulations. You guessed it!\n"
	        print "Do you want to play again? Type 'y' for yes and 'n' for no"
	        response = raw_input("> ")
		if 'y' in response:
		    start_up(guess_loop(ran_num()))
	        else:
		    print "Thanks for playing.  Goodbye."	
       		    exit()

            	
#Track guesses attempted and warn of end of game when chances
exhausted
def attempt_counter(start_number, the_number):
    print "That was attempt #%d." % (start_number)
    if start_number == 10:
        print "That was your last chance.  The correct answer was %d."
% (the_number)
        print "Do you want to start a new game. Type 'y' for yes and
'n' for no."
	answer = raw_input("> ")
        if 'y' in answer:
	    start_up(guess_loop(ran_num()))
	else:
	    print "Thanks for playing.  Goodbye."
            exit()



#function calls to start program
start_up()
guess_loop(ran_num())


More information about the Tutor mailing list