Slides badge

Conditional loops

Explain what is wrong with each program

for x = 0 to 5:
  print(x)
for x = 5 to 3:
  print(x)

Learning Intentions

  • Understand what a loop is in computer science
  • Implement a conditional loop in Python

Success Criteria

  • I can explain the benefit of loops in computer programming
  • I am able to develop a conditional loop in Python

Conditional loops

  • There are two kinds of loops: fixed loops and conditional loops
  • We've already looked at fixed loops and we've also already looked at conditions.

A conditional loop

  • A condition will always evaluate to True or False.
  • Conditional loops use a condition to loop whilst a condition evaluates to true.
  • Some conditional loops never evaluate to True and therefore are considered to be infinite loops.
  • Python has several types of conditional loops, but the one we are interested in is the while loop.

For each of the following, state whether it is true or false:

 

 

 

 

 

 

 

 

 

 

 

 

Task

x = True
y = False

print(x and y)
x = 54
y = 24

print(x > y)
x = 54
y = 24

print(x < y or y > 5)
x = True
y = True

print(not(x or y))

A while loop

  • While loops will continue to loop while a condition is True.
  • If the condition is initially false, then the loop will never run.
while True:
  print("This loop is never gonna end!")
n = 0
while n < 100:
  print(n)
  n = n + 1

A login system

print("Please insert your username")
username = input()
print("Please insert your password")
password = input()

if username == "Hamish" and password == "pass":
  print("Welcome!")
else:
  print("Login incorrect")

A login system - using loops

print("Please insert your username")
username = input()
print("Please insert your password")
password = input()
attempts = 0

while username != "Hamish" and password != "pass":
  print("Login incorrect")
  print("Please insert your username")
  username = input()
  print("Please insert your password")
  password = input()
  attempts = attempts + 1

print("Welcome", "It took you", attempts, "to login")

Conditional Loops

Complete the Conditional Loops worksheet

 

Complete the Python Conditional Loops (While) tasks in the Python book on page 31

Task

Presentation Overview
Close
JB
Conditional loops
© 2020 - 2024 J Balfour
17:23 | 23-05-2024
Join Live Session
Start Remote
Save Progress
Slideshow Outline
Presenter Mode
Widget Screen
Canvas Controls
Random Selector
Timer
Volume Meter
Binary Converter
Python Editor
Show Knox 90
Provide Feedback
Help
!
Keywords
    DragonDocs Management
    Random selector
    Sections
      Binary conversion
      Denary to binary conversion
      Binary to denary conversion
      Feedback 👍
      Accessibility

      Apply a filter:

      ×
      All slideshow files