# [Tutor] Error checking and such in a console "guess my number" game

Brett Wilkins lupin at orcon.net.nz
Tue Jan 8 05:58:05 CET 2008

```Hi there,

I'm been following a tutorial in a Python Programming book, making a
Guess My Number game.
I decided to go and modify it a bit more, therefore expanding it.

What I added in was the ability to modify the range of the pseudo-random
number generator, an exit strategy (i.e. type stop at the prompt),
something that keeps track of the amount of times you've gotten it
right, and error checking. Now, without further ado, here's the code
I've got:

#! /usr/bin/python

#imports area
import random

#main body
guessed_number = 0
stop = "n"
times_played = 0
range_for_random = 0
inter_range = ''
default = 100
print """
Welcome to

Guess My Number!

In this game you will attempt to guess a number that
the computer will psuedo-randomly generate, With
a range for which you dictate the ending point.
Good Luck!"""
while 1:
inter_range = raw_input("\n\n Please specify the range for the
randon number to reside within: ")

alphabet = "abcdefghijklmnopqrstuvwxyz"
intermediary_number = raw_input("\n\nPlease enter your guess, or
type stop to end game: ")

if intermediary_number == "stop":
exit()
elif 1:
for char in alphabet:
if char in inter_range:
range_for_random = default
continue
else:
if inter_range == '':
range_for_random = default
else:
range_for_random = int(inter_range)

while 1:
if times_played > 0:
stop = raw_input("\n\nTo end the game, type in stop, Or to
change the range, type change. Otherwise just press Enter:")
if stop.lower() == "change": break
elif stop.lower() == "stop": exit()
print "\n\nTime to start Guessing My Number!"
random_number = random.randrange(range_for_random) + 1

while 1:
alphabet = "abcdefghijklmnopqrstuvwxyz"
guess, or type stop to end game: ")

if intermediary_number == "stop":
exit()
elif 1:
for char in alphabet:
if char in intermediary_number:
guessed_number = 0
continue
else:
if intermediary_number == '':
guessed_number = 0
else:
guessed_number = int(intermediary_number)
if guessed_number > random_number:
print "\nYour guess was too high!"
elif guessed_number < random_number:
print "\nYour guess was too low!"
elif guessed_number == random_number:
print "\nYes, you got the right answer! The answer was:
" + str(random_number)
times_played += 1
print "You have now played %s time(s)." % (times_played)
break

Now I was wondering first off, is there a better way about doing the
error checking? I already tried try-except blocks, but as I'm new to
python (this is my second program after "Hello World") I don't know how
to only catch certain exceptions (in this case a ValueError). Using
try-except without limiting seems to disable Ctrl+C breaking in the
console, I found.

The next thing is my real problem:

if times_played > 0:
stop = raw_input("\n\nTo end the game, type in stop, Or to
change the range, type change. Otherwise just press Enter:")
if stop.lower() == "change": break
elif stop.lower() == "stop": exit()

What I want the break statement to do here is exit the while loop isn't
currently in, but it just seems to break the current if decision, making
it rather redundant. A little help with this in particular would be much
appreciated :)

And just so you know, I'm teaching myself python, for fun and for an
upcoming job, and this is just fun for me.

Cheers,

Brett

--
**************
Brett Wilkins
A.K.A Beedub
The Fixit

```