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

Malcolm 



# guess.py
# a simple number guessing game

import random

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

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)
    else:
        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
is?"

    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)
    else:
        print nope_message(random_num)


if __name__ == "__main__": 
    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,

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())
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list