Logic and Programming: How to Program a Simple Guess-the-Number Game in Python

Logic and Programming: How to Program a Simple Guess-the-Number Game in Python

In my Introduction to Logic classes, I am sometimes asked by students about the practical applications of formal logic. Although logic itself has many practical applications too numerous to list here, such as the way logic gates function in digital electronics (and gates, or gates, and so on), learning to think like a logician makes it easy to segue into any career field requiring formal or symbolic thinking, especially any form of coding, computer programming, or software engineering. This is because many of the concepts in formal logic, such as the concept of a conditional (an if/then statement), translate directly into computer programming.

I recently applied for an instructional design job in which I was asked to write some instructions for programming a basic video game as part of the application process. Since the simplest game I could think of was a guess-the-number game, and because the Python programming language has a very readable syntax, I decided to write up some instructions on programming a guess-the-number game in Python (version 3.x). I thought I would share the instructions for programming this simple game here on my website for those who are interested in learning logic concepts, learning to program, or in the connection between logic and programming.

In the following instructions you will learn a little programming, a little Python, and even a little formal logic in the form of conditional reasoning. Of course, this isn’t meant to be an exhaustive treatment of either programming or logic, not by a long shot, but it does show that some of the concepts in formal logic translate directly into learning computer programming and software engineering.

Getting Started

To introduce you to some of the basic concepts of programming, let’s make a simple “Guess the Number” game right off the bat. Even a simple text-based game like this will introduce you to two of the fundamental concepts of game programming, variables and randomization. Although you can use any programming language of interest to you, this example uses the popular Python programming language because of its simple and easy-to-understand syntax.

Part of the challenge of any engineering program, a video game or otherwise, and no matter how simple or how complex, is to break that program down into its essential components. For a simple Guess the Number game, we’ll need at least a few things:

  • A way to generate a random correct answer, say between 1 and 100

  • A way for the program to know what the correct answer is

  • A way for the user to input a guess

  • A way to check to see if the user’s input matches the correct answer

  • Feedback on the user’s guess (e.g., “Correct!”; “Too High!” “Too Low!”; etc.)

  • A way to try again if the user’s guess was incorrect

Let’s build a Guess the Number game from scratch to see how all of these pieces work together in a simple game.

First, let’s generate a random correct answer between 1 and 100 and store that correct answer in a variable called correctAnswer. A variable is like a container that holds some value. Each variable can hold only one value at a time:

Image Source: “Beginner’s BASIC” by Don Inman, Ramon Zamora, and Bob Albrecht, Copyright 1979, 1981 by Texas Instruments Incorporated.

Image Source: “Beginner’s BASIC” by Don Inman, Ramon Zamora, and Bob Albrecht, Copyright 1979, 1981 by Texas Instruments Incorporated.

In Python, you use the operator “=” to assign a value to a variable. For example, the statement “x = 5” assigns the value 5 to the variable x. So if you want to assign a specific number to the variable correctAnswer, you would use a statement like this:

correctAnswer = 47

We can also randomize the value of the correct answer using Python’s built-in random integer function “randint” like this:

correctAnswer = random.randint(1, 100)

This will automatically assign a random integer between 1 and 100 to the correctAnswer variable.

Next we need a way for the player to input a guess, and we’ll need to store the user’s guess as a variable so we can compare it to the correct answer. Let’s use a variable called playerGuess to store the player’s guess. The Python input function allows the player to input a guess (the “int” function converts the answer from a string of text to an integer):

playerGuess = int(input(“Guess a number between 1 and 100: “))

After the player has entered a guess, we’ll need to check to see whether the player’s guess is correct. To do this, we’ll create a variable called compareAnswer

If the player’s guess matches the correct answer, we’ll assign the value of “Correct” to compareAnswer. If the player’s guess is too high, we’ll assign the value of “High” to compareAnswer. And if the player’s guess is too low, we’ll assign the value of “Low” to compareAnswer. To do this, we’ll need to use a conditional, which is a type of “If…, then…” statement. 

Notice how the structure of the following code matches the line of reasoning just described:

if playerGuess == correctAnswer:

compareAnswer = “Right”

elif playerGuess > correctAnswer:

compareAnswer = “High”

elif playerGuess < correctAnswer:

compareAnswer = “Low”

The terms “if” and “else” should be fairly clear because they match how we use those terms in ordinary English. The term “elif,” however, is just Python’s way of saying “or else if.” Notice also the double equal sign ==. In Python a single equal sign = is used to assign a value to a variable, but a double equal sign == is used to compare two values and see if they are equal. Every programming language has its different quirks and slightly different syntax, and this is one of the quirks of Python!

So the first and second lines basically say “If the player’s guess equals the correct answer, then assign the value of “Right” to the compareAnswer variable.

The third and fourth lines basically say, “Or else, if the player’s guess is greater than the correct answer, then assign the value “High” to the compareAnswer variable.

And the fifth and sixth lines say, “Or else, if the player’s guess is less than the correct answer, then assign the value of “Low” to the compareAnswer variable.

One we have compared the player’s guess to the correct answer, we can use the print function to provide feedback to the player based on his or her response. Again, we’ll use a conditional (an “if” statement) to provide different feedback based on his or her guess:

if compareAnswer == “Right”:

print(“Correct! You Win!”)

elif compareAnswer == “High”:

print(“Too High! Guess Again!”)

elif compareAnswer == “Low”:

print(“Too Low! Guess Again!”)

The above code will allow the player to receive different feedback depending on whether the guess was correct, too high, or too low.

Finally, all we need to do is to allow the program to run continuously until the player guesses the correct answer. To do this, we will use a loop, which allows a piece of code to run over and over until some condition is met. Let’s define a variable called gameOver with an initial value of False. We can ask the player to keep inputting guesses as long as the value of gameOver is false. When the player guesses the correct answer, we can switch the value of gameOver to True and break out of the loop, ending the game.

To create a loop in Python, we’ll use the while function:

gameOver = False (Defines a variable gameOver with an initial value of False)

while gameOver == False: (Defines the condition for the loop to keep running)

[Here, indented, place the code that you want to loop]

 

The Finished Program: “Guess the Number”

Putting it all together, we have a program that looks like this:

# “Guess the Number”

# Programmed by Zachary Fruhling

# Copyright 2020

import random

correctAnswer = random.randint(1, 100)

gameOver = False


while gameOver == False:

playerGuess = int(input("Guess a number between 1 and 100: "))


if playerGuess == correctAnswer:

compareAnswer = "Right"

gameOver = True

elif playerGuess > correctAnswer:

compareAnswer = "High"

elif playerGuess < correctAnswer:

compareAnswer = "Low"


if compareAnswer == "Right":

print("Correct! You Win!")

elif compareAnswer == "High":

print("Too High! Guess Again!")

elif compareAnswer == "Low":

print("Too Low! Guess Again!")

 

Try the Finished Game!

Use the following link to try the finished game in your browser:

Video: Introduction to Philosophy Online Class Session — The Nature of Truth and Philosophy of Science

Video: Introduction to Philosophy Online Class Session — The Nature of Truth and Philosophy of Science

Don't Go Easy on Yourself; Be Hard on Yourself

Don't Go Easy on Yourself; Be Hard on Yourself