[Tutor] Guessing Game Program

Malcolm Newsome malcolm.newsome at gmail.com
Fri Mar 25 05:37:18 CET 2011

Hey Don!

I posted an eerily similar request to another python group about two weeks
ago!  I, too, am very new to programming and the guessing game was my first
shot at writing a script from scratch!

Below is my code (improved with some help from others).  I still would like
to make some improvements to it also.  But, perhaps there will be some ideas
in it that can help you as well!  Looking forward to learning and growing!

All the best!


# guess.py
# a simple number guessing game

import random

def nope_message(random_num):
    return "Nope! I'm smarter than you!\nI was thinking of the number: %d" %

def right_message(retried=False): 
    message = "That was right! I guess you ARE smarter than me"
    if retried:
        message += "... even though it took you another try!"

    return message

def reread_input(message):
    return int(input("You were too %s. Type another number: " % message))

def retry(message, random_num):
    guess_iflow = reread_input(message)

    if guess_iflow == random_num:
        return right_message(True)
        return nope_message(random_num)


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 1 - 100.  Can you guess what it

    random_num = random.randint(1, 100)

    guess = int(input("Type a number between 1 - 100: "))

    error = guess < 1, guess < 100

    if guess == random_num:
        print right_message()
    elif guess < random_num:# user gets second chance if number is too low
        print retry("low", random_num)
    elif guess > random_num:# user gets second chance if number is too high
        print retry("high", random_num)
        print nope_message(random_num)

if __name__ == "__main__": 

-----Original Message-----
From: tutor-bounces+malcolm.newsome=gmail.com at python.org
[mailto:tutor-bounces+malcolm.newsome=gmail.com at python.org] On Behalf Of
Donald Bedsole
Sent: Thursday, March 24, 2011 10:55 PM
To: tutor
Subject: [Tutor] Guessing Game Program

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,


#Author D.Bedsole
#drbedsole at gmail.com
#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
    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
                print "Thanks for playing. Goodbye!"
	    if  guess.isdigit(): #validate input
	        guess = int(guess)
	        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
	        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:
		    print "Thanks for playing.  Goodbye."	

#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
	answer = raw_input("> ")
        if 'y' in answer:
	    print "Thanks for playing.  Goodbye."

#function calls to start program
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:

More information about the Tutor mailing list