To make programs more efficient we can loop code (iteration) to repeat code sections over and over again.
This can save many lines of code by forcing repetitive tasks to be executed many times.
This makes programming a lot easier because:
Less lines of code means less mistakes.
Code is easier to maintain - you only need to make changes is one or two places.
Programs can be 'extended' beyond any hard restrictions to how many times an action may occur.
These loops continue repeating until the condition in the while statement is false.
It then exits from the loop and continues the code below it.
All code within a loop must have at least one tab / indent to ensure that whatever is indented is run (executed).
While condition is true
perform these action/s
#countdown
count = 10
while count > 0:
print("We are counting down from " + str(count))
input("Press enter.")
count = count - 1
#guessing game 1 (three guesses)
answer = 7
count = 1
while count <= 3:
userAnswer = int(input("Guess number " + str(count) + ". Guess a number between 1 and 10: "))
if userAnswer == answer:
print("Correct!")
count +=1
print("You didn't guess the answer in three tries.")
#guessing game 2
#note that for a != loop guessing game, you need to ask
#the question 'again' at the bottom of the loop
from random import randint
answer = randint(1,10)
tries = 0
guess = int(input("Make a guess"))
while guess != answer:
print ("Incorrect! Try again.")
guess = int(input("Make a guess"))
tries +=1
print("Well done, you guessed correct. You took",tries,"tries.")'
#this example could be VERY, VERY useful when completing your project this trimester!
#maths game 1
from random import randint
minimum = 10
maximum = 30
q = 1
while q <= 10:
num1 = randint(minimum,maximum)
num2= randint(minimum,maximum)
print ("Your paddock is",num1,"metres wide by",num2,"metres long. What is the perimeter of the paddock?")
perimeter = 2 * num1 + 2 * num2
userAnswer = int(input(">>> "))
if userAnswer == perimeter:
print("Correct")
score += 1
else:
print("Incorrect")
q += 1
#this example could be VERY, VERY useful when completing your project this trimester!
#maths game 2
from random import randint
minimum = 10
maximum = 30
q = 1
lives = 3
while q <= 10 and chances > 0:
num1 = randint(minimum,maximum)
num2= randint(minimum,maximum1,10)
print ("Your paddock is",num1,"metres wide by",num2,"metres long. What is the area of the paddock?")
correctAnswer = num1 * num2
userAnswer = int(input(">>> "))
if userAnswer == correctAnswer:
print("Correct")
score += 1
else:
print("Incorrect")
chances -= 1
q += 1
Iteration (Loops) - While Statements Video
FOR Loops repeat code for a set number of iterations. They use a counter variable to trace the number of times the block of code has been executed.
for x in range(0,5):
print(x)
#Prints out 0,1,2,3,4
#the first number is the starting point of the loop
# the second number is the number of times the counter will count to
for x in range(-2,4):
print(x)
#This example prints out -2,-1,0,1,2,3
#this example shows how the break function stops a loop when a condition is met
print("Example of a break function")
fruits = ["apple", "tomato", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break