This is an alternative edition of a game I modified the code for. See:

Guess My Number, Classic Edition

I seek advice on best practice. Tips, tricks, and suggestions, all welcome:

# Alex Grattan
# 9/10/14

# This is an alternative version of the "Guess My Number Game". 
# The Computer now has the role of guessing a number chosen by the player.
# The Computer emulates the expected behaviour of a player - 
# The Computer will not choose a number higher or lower than one already chosen.

import random

BOUNDS = (1, 100)
THE_NUMBER = int(input("\t\tPick a number between %r and %r:\n " % BOUNDS))

print("""\n\t\tThe Computer will now attempt to guess your number in a minimum of attempts:\n
\t\tO.K. Computer, you have %d gusses. Proceed!\n""") % TRIES_ALLOWED

for x in range(TRIES_ALLOWED):

    GUESS = random.randint(PREVIOUS_GUESSES.get('Min'), PREVIOUS_GUESSES.get('Max'))
    print ("\t\t\t %d\n") % GUESS
    TRIES += 1

        print("\t\tSorry Computer. Your guess of %d was too high.\n\t\tTake a lower guess:\n") % GUESS
        PREVIOUS_GUESSES['Max'] = GUESS - 1

    elif GUESS < THE_NUMBER:
        print("\t\tSorry Computer. Your guess of %d was too low.\n\t\tTake a higher guess:\n") % GUESS
        PREVIOUS_GUESSES['Min'] = GUESS + 1

        print("""\tCongratulations Computer, you guessed it! The number was %d
        \t\tAnd it only took you %d tries!\n""") % (THE_NUMBER, TRIES)
    print("""Sorry Computer; you failed to guess the number!
    The number I chose was %d""") % THE_NUMBER
  • 1
    \$\begingroup\$ I've just noticed; the variable 'PREVIOUS_GUESSES', should be changed to 'GUESS_BOUNDARIES'. \$\endgroup\$ Oct 10, 2014 at 20:36
  • \$\begingroup\$ Are you also looking for any suggestions to the strategy? \$\endgroup\$
    – wei2912
    Oct 11, 2014 at 15:32
  • \$\begingroup\$ I'm not sure what you mean by 'strategy'; but yes, sure. Any suggestions are welcome. \$\endgroup\$ Oct 12, 2014 at 15:22

2 Answers 2


A common practice is to reserve all capital names for constants. As these variables change during the execution of the program, so they should be lowercased instead: PREVIOUS_GUESSES, TRIES, GUESS.

Inside this else, you have a pointless statement:

    GUESS == THE_NUMBER  # <-- just delete this line... or at least comment it out

Using the .get method of dictionaries, for example in PREVIOUS_GUESSES.get('Min') and PREVIOUS_GUESSES.get('Max') can be useful when you're not sure if the keys (Min, Max in this example) exist, and if they don't exist you don't want a KeyError raised. In this script these keys are guaranteed to exist, so you can safely (and consistently) use PREVIOUS_GUESSES['Min'] and PREVIOUS_GUESSES['Max'] everywhere.


Unless you're using Python 3, DO NOT use the input() function. It'll interpret whatever they enter as code. Instead, use raw_input.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.