Search this site
Embedded Files
Software Engineering
  • Home
  • Preliminary
    • Programming Fundamentals
      • Project 0 - Intro
        • Software Development Process
        • Waterfall Vs Agile
      • Project 1 - Mad Libs
        • Lab 1 - Hello World
        • Lab 2 - Hello, name
        • Lab 3 - Hello, full name
        • Lab 4 - Split String
      • Project 2 - Temperature Converter
        • Lab 1 - Numbers
        • Lab 2 - Number Operations
        • Lab 3 - Formatting Numbers
        • Lab 4 - Conditionals
      • Project 3 - Guessing Game
        • Lab 1 - While Loops
        • Lab 2 - For Loops
        • Lab 3 - Input Validation
        • Lab 4 - Random
      • Project 4 - Dice Roller
        • Lab 1 - Functions
        • Lab 2 - Parameters and Arguments
        • Lab 3 - Try / Except
      • Project 5 - To Do List
        • Lab 1 - Lists
        • Lab 2 - Advanced List Features
        • Lab 3 - Errors
      • Project 6 - Shopping List
        • Lab 1 - Dictionaries
        • Lab 2 - Lists and Dictionaries
        • Lab 3 - Data Dictionaries
      • Project 7 - Tic Tac Toe
        • Lab 1 - Intro to Connect Four
        • Lab 2 - IPO Diagram and Screen Design
        • Lab 3 - Structure Chart and Modular Design
        • Lab 4 - Starting Development & Basic Gameplay
        • Lab 5 - Core Game Mechanics
        • Lab 6 - Win and Draw Conditions
      • Project 8 - Contacts
    • Object Oriented Programming
      • Project 9 - Contacts (OOP)
        • Lab 1 - Paradigms
        • Lab 2 - Classes
        • Lab 3 - Methods
        • Lab 4 - Contacts Manager
      • Project 10 - Animal Kingdom
        • Lab 1 - Generalisation & Inheritance
        • Lab 2 - Polymorphism
        • Lab 3 - Zoo Management System
      • Project 11 - Shapes & Geometry
      • Project 12 - Inventory System
        • Lab 1 - Encapsulation Revisited
        • Lab 2 - The Facade Pattern
        • Lab 3 - User Interface
      • Project 13 - Banking System
      • Project 14 - Library System
        • Lab 1 - Basic Text IO
        • Lab 2 - Introduction to JSON
        • Lab 3 - Library System
      • Project 15 - D&D Character Creator
    • Programming Mechatronics
      • Intro to Mechatronics
        • Applications
        • Components
        • Microbit
      • Computer Architecture
        • Microcontrollers vs CPUs
        • Instruction Sets and Opcodes
        • Registers and Real-Time Systems
      • Sensors and Data
        • Sensor Basics
        • Compass
        • Thermostat
        • Mapping and Clamping
        • Night Light
        • Parking Sensors
      • Actuators and Mechanics
        • Actuator Basics
        • Actuator Selection
        • DC Motors
        • Servos
        • Grippers
        • Pick and Place
      • Control Systems
        • Intro to Control Systems
        • Bang-Bang Control
        • PID Control
        • Autonomous Control
      • Power, Electrical and Wiring
        • Power Requirements
        • Electrical and Wiring
  • HSC
    • Programming For The Web
      • Command Line Basics
      • HTML
        • Task 1 - Basic Page
        • Task 2 - Contact Form
        • HTML Quiz
      • CSS
        • Task 1 - Style Basic Page
        • Task 2 - Style Contact Form
        • CSS Quiz
      • Bootstrap
        • Task 1 - Basic Bootstrap Page
        • Task 2 - Bootstrap Contact Form
        • Bootstrap Quiz
      • Flask
        • Task 1 - Installation
        • Task 2 - Routes
        • Task 3 - Templates
        • Task 4 - Flask Contact Form
        • Flask Quiz
      • SQLite
        • Task 1 - SQL Basics
        • Task 2 - SQLite Basics
        • Task 3 - SQLite and Flask
        • Task 4 - Object-Relational Mapping
        • SQLite Quiz
      • The Internet
        • Data Transfer
        • Web Protocols
        • Dev Tools
        • Securing the Web
        • Big Data
        • Internet Quiz
      • PWA Assessment Task
        • Progressive Wep Apps
        • Project Setup and Planning
        • Backend Development with Flask
        • User Interface and Design
        • Application Features
        • PWA Features
    • Secure Software Architecture
      • Introduction
        • Unsecure PWA
        • Vulnerabilities
        • Cybersecurity Concepts
        • Case Studies
      • Benefits
        • Data Protection
        • Confidentiality, Integrity, Availability
        • Minimising Cyber Attacks and Vulnerabilities
        • Authentication, Authorisation, Accountability
      • Software Development Steps
      • Influence of End Users
      • Security Features Incorporated Into Software
      • Cryptography and Sandboxing
      • Testing and Evaluating Security and Resilience
        • SQL Injection
        • Cross Site Scripting
        • Broken Authentication
        • Cross Site Request Forgery
      • Design, Develop and Implement Secure Code
    • Software Automation
      • Introduction
      • Labs
        • Numpy
        • Matplotlib
        • Scikit - Linear Regression
        • Scikit - Logistic Regression
        • Scikit - KNN
        • Scikit - Polynomial Regression
      • Training Models
        • Supervised Learning
        • Unsupervised Learning
        • Semi-Supervised Learning
        • Reinforcement Learning
      • Applications of Key ML Algorithms
      • Decision Trees and Neural Networks
        • Decision Trees
        • Neural Networks
      • Machine Learning Algorithms
        • Linear Regression
        • Logistic Regression
        • K-Nearest Neighbour
      • Impact of Automation
    • Software Engineering Project
  • AI Tools
  • Software
  • Syllabus
  • Exam
Software Engineering

PROJECT 2 - TEMP CONVERTER

LAB 4 - Conditionals

Lab 4 Overview

Let's jump into something different now. How to allow our programs to make decisions, or change their flow based on different input.

TASK 1

  1. Open lab4.py from Project 2 - Temp Converter

Checking a Password

As a first example, how might a program check if a user's password is correct?
Here we introduce the if statement along with the comparison operator ==
Essentially this reads: If the password equals "hunter2" then print out "Correct!"

But it doesn't provide the user with any feedback on an incorrect password. Nothing happens at all because the print statement will only run if the condition password == "hunter2" is true. What do we do about if it is false? Here we introduce the else statement.

A note about the equals signs:

  • = and == are different. 

  • We use = to assign a value to a variable. Hence it is called the assignment operator.

  • We use == to compare two values. Hence it is called the comparison operator.

Indentation

In the examples above you might have noticed that the print statements were indented across (two spaces).
This is an important concept in Python. Have a look at what happens below when we don't use indentation correctly.

We want to print out "Welcome to the secure area." on a new line if the password is correct. However, it also prints out if it is incorrect.
This is because it is inline with the main program and not the if statement. Unlike many other programming languages that use braces {} to define code blocks, Python uses indentation to indicate which lines of code belong together in the same block.

Comparison Operators

In Python, there are several comparison operators to evaluate the relationship between two values:

  • == checks if two values are equal.

  • != checks if two values are not equal.

  • < checks if the value on the left is less than the value on the right.

  • > checks if the value on the left is greater than the value on the right.

  • <= checks if the value on the left is less than or equal to the value on the right.

  • >= checks if the value on the left is greater than or equal to the value on the right.

Elif

Notice in the above example the numbers you enter will satisfy multiple conditions. How could we then do something more complex then, like classify grades as 'High Distinction', 'Distinction', 'Credit', 'Pass', and 'Fail' based on numerical scores? 

Here we introduce the elif statement as a way to handle multiple conditions. The else statement still catches anything 'else'.

One of the important concepts here is that only the first condition that satifies the if-elif-else chain will execute. Once a condition is satisfied, the remaining conditions in the chain are skipped. Ordering your conditions properly is crucial for your program to function as intended.
Let's have a look at a common mistake that occurs. Try entering a high score such as 90 which should receive a 'Distinction'.

Logical Operators

Often we may need to evaluate more than one condition to make a decision. For instance, what if we want to check whether a user's age is within a particular range? Here we introduce logical operators that allow us to combine multiple conditions.

  • and: Checks if both conditions are True.

  • or: Checks if at least one condition is True.

  • not: Negates the condition following it.

Say you want to verify whether the user's age is between 18 and 65. 

You would need to check two conditions: the age should be greater than or equal to 18 and less than or equal to 65. Here, the and operator is helpful.

Sometimes only one of the conditions needs to be true. Let's say we want to check if a person qualifies for discount if they are a student or under 18.

Here, we use the or operator.

Try some different combinations of inputs to test it out.

TASK 2

  1. Complete the following requirements:

    • Ask the user for an integer and store it in a variable 'num'

    • Use an if statement to check if the number is positive, negative or zero

    • Ask the user for their age and store it in a variable 'age'

    • Use an if statement to check if the user is an adult (18+) or a minor

    • Ask the user for two integers, 'a' and 'b'

    • Use an if statement to check if both numbers are positive or both are negative.
      Otherwise, print "One number is positive and the other is negative."

  2. Press Ctrl + F5 to run the program or open a terminal and type python lab4.py

  3. It should run without errors and you should see something like on the right:

Example:

Please enter an integer: -7

The number is negative.

Please enter your age: 19

You are an adult.

Enter the first integer (a): -3

Enter the second integer (b): 5

One number is positive and the other is negative.

You should now know how work with conditional statements to make decisions and control the flow of your code.
Let's wrap up this lab by pushing our code to GitHub:

TASK 3

  1. Enter a commit message. E.g. "Lab 4 complete"

  2. Press Commit to main

  3. Press Push origin

Video

Here is a video from Harvard's CS50P course that covers the concept of conditionals in much more detail. Optional, but highly recommended!

Report abuse
Page details
Page updated
Report abuse