1
\$\begingroup\$

Based on the many other number guessing games people tend to post as first questions, https://www.xkcd.com/221/, and a crypto class I'm taking, I've decided to implement a number guessing game using the best python style I can, and the best random number generator I can create. Any advice is appreciated.

#!/usr/bin/env python3

"""
This is a simple guessing game. It is very difficult.
"""

from time import time


class Random():
    '''
        Class for generating cryptographically secure random 4s
        4 is random and was chosen by dice roll.
    '''

    def __init__(self, seed=0):
        self.seed = seed

    def set_seed(self, seed):
        '''
            Sets the rng's seed.
            This method should be called before the first use if a seed is not
            passed to the constructor
        '''
        self.seed = seed

    def get_random_int(self, low, high):
        '''
            Generates a random integer
            between low and high as long as low <= 4 <= high.
        '''
        return self._restrict_to_bounds(4, low, high)

    def get_random_float(self, low, high):
        '''
            Generates a random float
            between low and high as long as low <= 4.0 <= high.
        '''
        return self._restrict_to_bounds(4.0, low, high)

    def _restrict_to_bounds(self, val, low, high):
        ''' returns val clamped between low and high'''
        if low <= val <= high:
            return val
        if val == 4:
            return val
        return 4

def game(low=0, high=1_000_000):
    ''' Runs the game '''

    rand_source = Random(time())
    secret_number = rand_source.get_random_int(low, high)

    print(f'''Welcome to the Guessing Game.
I have generated a random integer between {low}, and {high}.
Your job is to guess it in as few guesses as possible.\n''')

    guess_num = 1
    guess = None
    while True:
        guess = input('Enter your guess: ')
        try:
            guess = int(guess)
        except ValueError:
            print("You didn't guess an integer, try again.")
            continue

        if guess == secret_number:
            break
        elif guess > secret_number:
            print('Your guess was too high, try again')
        elif guess < secret_number:
            print('Your guess was too low, try again')
        guess_num += 1

    print()
    if guess_num == 1:
        print(f''''You got the right answer in {guess_num} guess!
Are you cheating?''')
    else:
        print(f'You got the right answer in {guess_num} guesses!')

    play_again = input('Do you want to play again? (enter y or n)\n')
    if play_again == 'y':
        game()

if __name__ == '__main__':
    game()
\$\endgroup\$
  • \$\begingroup\$ “the best random number generator” for “generating cryptographically secure random 4s” – is this meant as a serious request for review? \$\endgroup\$ – Martin R Oct 13 '18 at 10:15
  • \$\begingroup\$ I'm not really sure. It's definitely silly, but that was kind of the point. \$\endgroup\$ – Oscar Smith Oct 13 '18 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.