A Closer Look at the Booleans Conditional Statement and Loops

A Closer Look at the Booleans Conditional Statement and Loops

Learn to build an AI Rock, Paper, Scissors Game With booleans, conditional statements and loops.

Introduction

A Boolean is a data type that can have only the values true or false. They are named after the mathematician George Boole, who first described a system of logical operations based on true and false values. Loops on the other hand allow the execution of a block of code repeatedly until a certain condition is met. In this article, we will explore the usage of booleans and Loops in programming and use it to create a Rock Paper Scissors game.

Uses of Booleans

Booleans are often used to control the flow of programs, make decisions, and express logical statements.

syntax

x = True
y = False

Boolean Operators

Operators are used in performing operations on variables and values. Booleans supports several logical operations that are used to compare, combine, and manipulate boolean values. The most common operations include:

  • AND (and): Returns True if both operands are True

  • OR (or): Returns True if either operand is True

  • NOT (not): Returns the opposite boolean value of the operand

Examples

x = True
y = False

print(x and y)   # False
print(x or y)    # True
print(not x)     # False

Comparison operators can also be used to create boolean expressions that evaluate to True or False. The most common comparison operators include:

==

Equal

x == y

!=

Not equal

x != y

>

Greater than

x > y

<

Less than

x < y

<=

Less than or equal to

x <= y

>=

Greater than or equal to

x >= y

Python Loops

Loops are programming concepts that repeat a portion of code a set number of times until the desired process is complete.

Types of Loops

Python has two primitive loop commands:

  • for loops

  • while loops

  1. For Loop

A for loop is used to iterate a specific number of times and is commonly used with arrays or lists. The syntax of a for loop is as follows:

#Print each fruit in a fruit list
fruits = ["apple", "banana", "mango"]
for x in fruits:
  print(x)
  1. while Loop

A while loop is used to execute a block of code repeatedly while a certain condition is true. The syntax of a while loop is as follows:

#Prints all te numbers less than 6
i = 1
while i < 6:
i += 1
  print(i)

Rock Paper Scissors Game.

Rock, paper, scissors (also known as Rochambeau, Roshambo, or Janken) is a fun and easy game that anyone can learn and enjoy. It’s a great way to make minor decisions when you and a friend can’t agree on something or even just an entertaining way to pass the time. We are going to create a rock-paper-scissors game with python.

import random

random_choice = random.randint(0,2)

#getting winner
winner = ''


#getting the computer's choice

if random_choice == 0:
   computer_choice = 'rock'
elif random_choice == 1:
   computer_choice = 'paper'
else: 
   computer_choice = 'scissors'

#getting the users choice and validating for incorrect input with while loop.
user_choice = ''
while (user_choice != 'rock' 
         and user_choice != 'paper'
         and user_choice != 'scissors'):
      user_choice = input('rock, paper, scissors?' )  

#logic behind the game
if computer_choice == user_choice:
    winner = 'Tie'
elif computer_choice == 'paper' and user_choice == 'rock':
     winner = 'computer'
elif computer_choice == 'rock' and user_choice == 'scissors':
     winner = 'computer'
elif computer_choice == 'scissors' and user_choice == 'paper':
     winner = 'computer'
else:
     winner = 'user'  

# display the winner  
if winner == 'Tie':
     print('we both choose', computer_choice, 'play again!')
else:
     print(winner, 'won. computer choose', computer_choice + '.')

This is the source code for a simple Rock-Paper-Scissors game. It randomly generates the computer's choice and gets the user's choice through an input prompt, while also validating for incorrect inputs using a while loop. The code then compares the choices and determines the winner, displaying the result through a print statement.

conclusion

Booleans are an essential data type in programming that represent logical values of "true" or "false". They are used in many contexts to control program flow, make decisions, and express logical statements. Loops are programming concepts that repeat a portion of code a set number of times until the desired process is complete. Understanding how to use and manipulate booleans and loops is an important skill for any programmer.