3.3. If statements#

If statements are used in a program to execute commands when a certain logical condition is met. The syntax of an if statement takes the following form

if condition:
    commands executed when true

Note that the commands following the if statement are indented. Python assumes that the indented lines following the if statement full under the scope of the if statement which ends with the first non-indented line.

Enter the following code into your program.

# If statements
x, y = 2, 3

if x < y:
    print(f"\n{x} is less than {y}")

Run your program and your should see the following added to the console output.

2 is less than 3

Change the value of x so that it is greater than 3 and run your program again. Is the result what you expected?


3.3.1. Else-if#

Sometimes you want to perform calculations if the previous conditions were not true. We can do this using the if-else condition with the elif keyword.

if condition1:
    commands executed if condition1 is true

elif condition2:
    commands executed if condition2 is true

Enter the following code into your program.

# Else-if statements
hour = 15

if hour < 12:
    print("Good morning, how are you today?")

elif hour < 18:
    print("Good afternoon, are you having a good day?")

Run your program and your should see the following added to the console output.

Good afternoon, are you having a good day?

3.3.2. Else#

The else keyword is used to perform calculations if all previous conditions are false.

if condition1:
    commands executed if condition1 is true

elif condition2:
    commands executed if condition 2 is true
    
else:
    commands executed if all previous conditions are false

Add the following code to the end of your if-else if statement.

else:
    print("Good evening, did you have a good day")

Change the value of the hour variable to 21. Run your program and your should see the following added to the console output.

Good evening, did you have a good day

3.3.3. Exercises#

Exercise 3.1

The degree classification for an undergraduate student at Manchester Metropolitan University is determined using by calculating a weighted average of the level 5 (2nd year) and level 6 (3rd year) unit marks using the ratio 25:75 then looking up the classification in the table below.

Weighted average

Degree classification

\(\geq 70\)

first class (1st)

\(\geq 60\)

upper second class (2:1)

\(\geq 50\)

lower second class (2:2)

\(\geq 40\)

third class (3rd)

\(<40\)

fail

For example, a student who has gained level 5 marks of 55, 45, 75, 65 and level 6 marks of 60, 74, 72, 68 has a weighted average of 66.4 and therefore will graduate with upper second class honours.

Create a new Python file called 3_If_statements_exercise.py and write a program that prints the degree classification based on the level 5 and 6 marks that are stored in two arrays. Use your program to determine the degree classification of a student who has level 5 marks of 62, 65, 72, 64 and level 6 marks of 66, 73, 77, 70.

Exercise 3.2

The degree classification of a student is also determined using the profile method that only uses the level 6 marks. The average of the level 6 marks and the degree classification is found by looking up the table below.

Level 6 average

together with two units marks of

Degree classification

\(\geq 68\)

\(\geq 70\)

first class (1st)

\(\geq 58\)

\(\geq 60\)

upper second class (2:1)

\(\geq 48\)

\(\geq 50\)

lower second class (2:2)

\(\geq 40\)

\(\geq 40\)

third class (3rd)

The degree classification a student receives is the higher classification of the weighted average method and the profile method.

For example, for the same student with level 6 marks of 60, 74, 72, 68 giving a level 6 average of 68.5. Since they have 2 units with marks above 70% the classification using the profile method is first class honours.

Extend your program from Exercise 3.1 so that the degree classification using the profile method is determined and the final degree classification is printed.

Hint: The np.sort(x) function sorts the NumPy array x into ascending order.

Exercise 3.3

Write a program that plays the rock-paper-scissors game. Your program should select from rock, paper or scissors at random and based on a choice you have made determine who has won and prints an appropriate message to the console as follows.

Exercise 3.3
------------
You have chosen rock
Your opponent has chosen scissors

Rock crushes scissors, you win!

Hint: the NumPy function np.random.choice(<array>) choses a random element from an array.