Guess the Number Game

The objective of the game in the book is to simply guess the random number chosen between a set limit. I took it a step further lets see how I did it.

The end result of the program :

I am thinking of a number from  randfrom : 3  to  randto : 8  .
How many times would you like to guess:
1
Take a guess: 
1
Your number is lower...
Try Again  secretnum  :  6

Process finished with exit code 2
I am thinking of a number from  randfrom : 3  to  randto : 10  .
How many times would you like to guess:
2
Take a guess: 
10
Your number is higher..
Take a guess: 
3
Your number is lower...
Try Again  secretnum  :  5

Process finished with exit code 2
I am thinking of a number from  randfrom : 3  to  randto : 8  .
How many times would you like to guess:
1
Take a guess: 
1
Your number is lower...
Try Again  secretnum  :  6

Process finished with exit code 2

Github for the code: https://www.github.com/sahilkapila

Getting Started

At the top of my code I begin with importing a couple of modules :

  1. The random module is used in the program as I need to generate a random set of integers using the randint() sub function.
randfrom = random.randint(1,3)
randto = random.randint(8,10)

secretnum = random.randint(randfrom,randto)
  1. The sys module is used in the program as I was trying to experiment with the cool message at the end of program execution, in the console. Trust me, in retrospect, it was childish but oh well. So, I was trying to use these exit codes (sys allows you to modify these!) as a check at the end of program execution.
if guess == secretnum:
    print("Good Job ",nameof(guessesmade)," : ",guessesmade)
    sys.exit(1)
  1. The varname module is an interesting one. I was wondering if whilst I print the values of the check flags used in the program can I print their names (In pursuit of logical variable names!), turns out YES 🙂
print("I am thinking of a number from ",nameof(randfrom),":",randfrom," to ",nameof(randto),":",randto," .")

The Logic

Now lets understand the purpose the purpose of this code, the intended outcome if you will.

  1. Let the user know the guessrange (the range of number between which the computer is guessing).
print("I am thinking of a number from ",nameof(randfrom),":",randfrom," to ",nameof(randto),":",randto," .")
  1. Ask the user how many times they would want to guess.
print("How many times would you like to guess:")
guess = 0
numberofguesses = int(input())
  1. Give a hint on whether your guess is lower or higher to picked number.
    if guess > secretnum:
        print("Your number is higher..")
  1. Prompts user when match with exit code (1).
if guess == secretnum:
    print("Good Job ",nameof(guessesmade)," : ",guessesmade)
    sys.exit(1)
  1. Prompts user to try again with exit code(2). Sidebar, remember by default the program terminates with exit code(0).
else:
    print("Try Again ",nameof(secretnum)," : ",secretnum)
    sys.exit(2)

Final Thoughts

This was a challenging one and took some thinking and research. It was interesting to find out how to get a variable name to print on the console and messing with the exit codes.

Found a book and following along:

https://automatetheboringstuff.com/

About: sahilkapila