IF-THEN: Creating a Jackpot Game

Sergio Ribeiro, June 2021

All of us are somewhat familiar with the expressions logical reasoning, logical thinking, or logical conclusion. Naturally, each of us will try to explain these expressions in our way. But possibly, all explanations will tend to reach the same understanding. Those are just different ways of expressing the same idea.

The preoccupation with thinking, reasoning, and language comes from a long time ago. We cannot attribute to any human being the creation of language, reason, or even logic. These are elements that are part of our human constitution. However, we can identify some people who prominently dedicated themselves to its study. Aristotle was one of them.

His work on formal logic was vital for the development of studies in argumentation, mathematics, and computing. IF-THEN arguments, also known as conditional arguments or hypothetical syllogisms, are the basis of deductive logic.

IF p THEN q is the standard way of representing an IF-THEN argument, where p is known as the antecedent and q the consequent. That is, q only happens when p is true:

IF it doesn’t rain (p), THEN John doesn’t go to work (q).

John goes to work as a consequence of p.

This logical structure helps to conclude that:

John did not go to work, so it rained.

It is a small example that helps us to see how to use an IF-THEN argument. This type of argument is the norm in computing. IF-THEN is widely used in the construction of electrical circuits that are the basis of what we call hardware—also used by the various programming languages to build programs or software, essential for the functioning and operation of hardware.

One thing that makes a computer superior to an ordinary calculator is its ability to make decisions. And this is done through the IF-THEN argument in a control structure. This structure allows a program to branch out in different directions, executing further instructions according to a specific test result.

Example in Python

In Python, we use IF to create this control structure. See the example below:

if INCOME > 5000:
  print("Loan approved")

Note in the example given that the consequent (q), which means having the loan approved, depends on the income being greater than five thousand. The condition that checks the INCOME is the antecedent (p).

We can improve this program using IF-ELSE, which determines that one command block’s execution or another is according to the condition. See the example below:

if INCOME > 5000:
  print("Loan approved")
else:
  print("Loan denied")

In this second example, if the condition INCOME > 5000 is true, then the “Loan approved” message is printed. Otherwise, the message printed is “Loan denied.”

Creating a Jackpot Game

Let’s create a little Jackpot game to illustrate this. The rules for our game are:

  • You start with a credit of $10.00
  • You must pull the machine crank to rotate 3 internal roulette wheels
  • Each of the roulette wheels has 6 fruits
  • The result of each of the roulette wheels is shown on the machine’s panel, and there are three displays
  • If at the end of the spin of the roulette wheels, each of the dials on the panel shows the same fruit, you earn another $10.00
  • If the first two or the last two displays on the panel show the same fruit, you earn $2.00
  • If each of the displays shows a different fruit, you lose $1.00

Below is the final code of the game. Note that we are making use of the random library discussed previously in another article.

import random

credit = 10
play   = True
fruits = ["orange","pinaple","strawberry","grape","banana","water-melon"]

while play:
  
  d1 = random.choice(fruits)
  d2 = random.choice(fruits)
  d3 = random.choice(fruits)
  
  print("Credits:",credit)
  print("--------------------")
  print(d1,d2,d3)
  print("--------------------")

  if d1 == d2 and d2 == d3:
    print("WOW! You have hit the Jackpot and won $10.00")
    credit+=10
  elif (d1 == d2 or d2 == d3) and d1 != d3:
    print("Congrats! You have won $2.00")
    credit+=2
  else:
    print("Sorry! You have lost $1.00")
    credit-=1
    
  if credit <= 0 or input("Another go Y/N?") not in "Yy":
    play = False

print("GAME OVER")
print("Balance-->",credit)

Take the opportunity to test the program and make any changes you want.

If you don’t already know Python programming language and would like to start programming in Python, take a look at Computers and Information Processing for Business: Microsoft Office 2019 and Python.