LESSON 02 –
CONDITIONAL STATEMENTS

 

 

LESSON NOTE/GUIDE

PROGRAM 1 – MAX OF TWO NUMBERS

·         Open your previous project, and create a directory (folder) for lesson 2.

·         Add a new python file into lesson 2.

·         Write the program that gets two numbers from the user and outputs the greatest one by using the built-in max function.

 

THE CODE

 

n1 = int(input("Enter a number"))

n2 = int(input("Enter another number"))

 

largest = max(n1,n2)

print(largest)

 

EXPLANATION

 

The first two lines simply get two integers from the user and store them in n1 and n2.

 

On the third line, the max function is used to find the maximum number between n1 and n2.  It’s result is stored in the variable largest.

 

We then print largest out in line 4.

 

ALTERNATIVE CODE

 

n1 = int(input("Enter a number"))

n2 = int(input("Enter another number"))

print(max(n1,n2))

 

EXPLANATION

 

The first two lines are the same as in the first code segment. 

 

The third line prints out the result of the max function immediately.  This is shorter and doesn’t require a third variable.

 

 

PROGRAM 2 – BIGGER NUMBER

  • Write the program above with an if statement instead.
  • Include an else statement.
  • Consider how indentation is required.
  • Use the greater flexibility of this approach to display a message such as: “7 is greater than 5”

 

THE CODE

 

a = int(input("Enter a number"))

b = int(input("Enter another number"))

 

if a > b:

    print(a, "is bigger than", b)

else:

    print(b, "is bigger than", a)

 

EXPLANATION

 

The first two lines take care of user input.

 

The third line has the condition a > b which and is read

 

if a is greater than b

 

When the condition is true, then the statement(s) that is/are indented under the if line are executed.  If the condition is false, those statements are skipped.

 

In the situation where an else line is included, the indented statements under the else are executed only if the condition in the if above is false.

 

Note:  This program fails when the two numbers are equal.  More on this later on.

 

PROGRAM 3A – PASS OR FAIL

  • Write the program that asks the user for a mark and responds by outputting either pass or fail based on the mark.

 

THE CODE

 

mark = float(input("Enter your mark."))

 

if mark < 50:

    print("You are failing.")

else:

    print("You are passing.")

EXPLANATION

 

The first line deals with the user input.  Notice that this gets a float number from the user.

 

The condition in the if statement tests if mark is below 50.  If it is, the condition is true and the first print statement is executed.  If the mark is above 50, then the condition is false and the second print statement is executed.

 

PROGRAM 3B – PASS BY OR FAIL BY

  • Alter the above program to include the amount by which the user is passing or failing.

 

THE CODE

 

mark = float(input("Enter your mark."))

 

if mark < 50:

    failBy = 50 - mark

    print("Failing by", failBy)

else:

    passBy = mark - 50

    print("Passing by", passBy)

EXPLANATION

 

The first line deals with the user input.

 

When the mark is under 50, the two indented statements under the if line are executed.  The first of these statements calculates the amount by which the user is failing and the other outputs a message to the screen.  The indented statements under the else are skipped.

 

When the mark is 50 or over, the two lines under the if line are skipped and the bottom two lines are executed.  These lines calculate the amount by which the user is passing and that is outputted to screen.

 

PROGRAM 4A – RANDOM NUMBER

  • Generate a random number.

 

THE CODE

 

import random

 

num = random.randint(1,10)

print(num)

EXPLANATION

 

The first line tells Python that this program will be using functions (commands) that are located inside the library called random.

 

The second line uses the randit function (which is short for random integer) to generate a random integer between 1 and 10.  That number gets stored in the variable num.

 

The third line simply outputs that value.

 

Try running this program over many times to verify that the value is in fact random.

 

PROGRAM 5 – COIN FLIP

  • Make use of a random number to create a program that generates either the messages “heads” or the message “tails”.

 

THE CODE

 

import random

 

rn = random.randint(1,2)

 

if rn == 1:

    print("heads")

else:

    print("tails")

EXPLANATION

 

The first line tells Python that this program will be using functions (commands) that are located inside the library called random.

 

The second line generates a random number between 1 and 2 and stores it in the variable rn.

 

The if statement checks if rn is 1, and if it is, it outputs “heads”.  Otherwise, it outputs “tails”.

 

PROGRAM 6 – RANDOM GREETING

  • Make use of a random number to create a program that generates a random greeting for the user after they give you their name.

 

THE CODE

 

import random

 

name = input("Enter your name")

 

rn = random.randint(1,4)

 

if rn == 1:

    print("Hello", name)

if rn == 2:

    print("Oh hi", name)

if rn == 3:

    print(name, "!  Go away!")

if rn == 4:

    print("What's up fool?")

EXPLANATION

 

The first line imports the random library.

 

The second line gets a name from the user and stores it in name.

 

The third line generates a random number between 1 and 4 and stores it in rn.

 

The four if statements check which value is stored in rn and executes the corresponding print statement.

 

It should be noted that these are four different if structures.  Next we will learn how to merge them into one structure.

 

 

PROGRAM 7 – BIGGER NUMBER II (WITH EQUALITY)

  • Program 2 compared two numbers inputted by the user. Improve it by including a message for when the numbers are equal.  Make use of elif so that we have only one structure.

 

THE CODE

 

a = int(input("Enter a number"))

b = int(input("Enter another number"))

 

if a > b:

    print(a, "is bigger than", b)

elif a == b:

    print(a,"and",b,"are equal.")

else:

    print(b, "is bigger than", a)

 

EXPLANATION

 

The first two lines get the user input.

 

The first condition in the if checks if a is greater than b.  If it is, then the first print statement is executed and the rest of the if structure is skipped.

 

If the first condition is false, then the second condition, if a is equal to b, is checked.  If that is true, then the second print statement is executed and the rest of the if statement is skipped.

 

If the second condition is false, then we reach the else and the final print statement is executed.

 

It is important to note that only one statement block is ever executed in an if structure.

 

 

PROGRAM 8 – LEGAL VALUE?

  • Ask the user for a number from 1 to 25.  If the number is If the number is out of range, output the message “Illegal number”.  Otherwise, output “Legal number”.
  • Consider a solution using or and a solution using and.

 

THE CODE (USING or)

 

m = int(input("Enter a number (1 to 25)"))

 

if m < 1 or  m > 25:

    print("Illegal number")

else:

    print("Legal number")

EXPLANATION

 

The first line of code gets the user input.

 

The if statement has two conditions merged together with an or operator.   For the or operator, as long as one of the two conditions is true, the overall condition will be true.

 

So, if m is less than 1 or if m is greater than 25, the overall condition will be true and the program will output “Illegal number”.

 

If the overall condition is false, then the print statement in the else section will be executed.

 

THE CODE (USING and)

 

m = int(input("Enter a number (1 to 25)"))

 

if m >= 1 and  m <= 25:

    print("Legal number")

else:

    print("Illegal number")

EXPLANATION

 

The first line of code gets the user input.

 

The if statement uses an and operator to merge two conditions together.  For and operators, both conditions need to be true for the overall condition to be true.

 

So, if m is greater or equal to 1 and m is lesser or equal to 25, the overall condition will be true and “Legal number” will be outputted.

 

Otherwise, the “Illegal number” will be outputted.