Python || Random Number Guessing Game Using Random MyRNG & While Loop

Here is another homework assignment which was presented in introduction class. The following is a simple guessing game using commandline arguments, which demonstrates the use of generating random numbers.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

How To Get User Input
Getting Commandline Arguments
While Loops
MyRNG.py - Random Number Class

==== 1. DESCRIPTION ====

The following program is a simple guessing game which demonstrates how to generate random numbers using python. This program will seed the random number generator (located in the file MyRNG.py), select a number at random, and then ask the user for a guess. Using a while loop, the user will keep attempting to guess the selected random number until the correct guess is obtained, afterwhich the user will have the option of continuing play or exiting.

==== 2. USAGE ====

The user enters various options into the program via the commandline. An example of how the commandline can be used is given below.


python3 guess.py [-h] [-v] -s [seed] -m 2 -M 353

Where the brackets are meant to represent features which are optional, meaning the user does not have to specify them at run time.

The -m and -M options are mandatory.

• -M is best picked as a large prime integer
• -m is best picked as an integer in the range of 2,3,..,M-1

NOTE: The use of commandline arguments is not mandatory. If any of the mandatory options are not selected, the program uses its own logic to generate random numbers.

==== 3. FEATURES ====

The following lists and explains the command line argument options.

-s (seed): Seed takes an integer as a parameter and is used to seed the random number generator. When omitted, the program uses its own logic to seed the generator

-v (verbose): Turn on debugging messages.

-h (help): Print out a help message which tells the user how to run the program and a brief description of the program.

-m (minimum): Set the minimum of the range of numbers the program will select its numbers from.

-M (maximum): Set the maximum of the range of numbers the program will select its numbers from.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output:

Seed = 806189064, Minimum = 1, Maximum = 1000

I'm thinking of a number between 1 and 1000. Go ahead and make your first guess.
>> 500

Sorry that was not correct, please try again...

>> 400

WARMER

>> 600

COLDER

>> 300

WARMER

>> 150

WARMER

>> 100

COLDER

>> 180

COLDER

>> 190

COLDER

>> 130

WARMER

>> 128

WINNER! You have guessed correctly!
It took you 10 attempt(s) to find the answer!

Would you like to play again? (Yes or No): y

------------------------------------------------------------

Make a guess between 1 and 1000

>> 500

Sorry that was not correct, please try again...

>> 600

COLDER

>> 400

WARMER

>> 300

WARMER

>> 280

WARMER

>> 260

WARMER

>> 250

COLDER

>> 256

WINNER! You have guessed correctly!
It took you 8 attempt(s) to find the answer!

Would you like to play again? (Yes or No): n

Thanks for playing!!

Was this article helpful?
👍 YesNo

Leave a Reply