[Tutor] Re: Please critique my guessing game program. (original) (raw)

Matt Smith smith-matt at tiscali.co.uk
Thu Jul 15 18:04:35 CEST 2004


Hi, Thanks fo the help, I've made some changes to the program (please see below) and I intend to write a function to test the type of the variable guess (will have to use raw_input insted of input).

I've got a couple of queries at this stage of developing the program:

  1. Would it be preferable to have the main game loop as part of the main body of the program at this stage? At the moment it doesn't seam logical to me to split off the section that I have as a function.

  2. Should I use the newline character in my print statements as opposed to having empty print statements? I guess this would look neater and take up less lines of code.

Thanks again for the help, Matt.

A guessing game program.

By Matt Smith

import random

def Random100(): """function to return a random number between 1 and 100""" return random.randint(1,100)

Game loop.

def Game(guess): guesses = 1 while guess != number: if guess > number: guess = input("Too high, try again. ") else: guess = input("Too low, try again. ") guesses = guesses + 1 print "Well done, you got it in",guesses,"goes."

Main program.

Introduce the program.

print print "" print "* Guessing Game *" print "" print

Play the game.

playagainflag = 1 while playagainflag == 1: print print "I am thinking of a number between 1 and 100" number = Random100() guess = input ("What's your first guess? ") Game(guess) print playagain = raw_input("Do you want to play again? (y/n) ") if playagain == "y" or playagain == "Y": playagainflag = 1 else: playagainflag = 0 print "Goodbye!" print

End of program.



More information about the Tutor mailing list