A program to count from 1 to 5
n, delta = 1, 1
print(n)
n = n + delta
print(n)
n = n + delta
print(n)
n = n + delta
print(n)
n = n + delta
print(n)
Um. That was kind of tedious. Can't we just tell the computer to repeat those two lines of code?
YES! …using loops
n = 1
end = 5
delta = 1
while n <= end:
print(n)
n = n + delta
Some formal definitions:
Using a while loop allows us to:
A template:
# while <some sort of condition>:
# <do stuff here>
Some real code:
a = 100
while a > -1:
print(a)
What does this output? →
100
100
100
What do these snippets of code print out?→
while True:
print("I'm true!")
while False:
print("I'm false!")
I'm true!
I'm true!
I'm true!
# nothing printed here
while True:
print("I'm true!")
while False:
print("I'm false!")
We never even get into the body of the loop!
What does this print out? →
keep_on_going = True
while keep_on_going:
print("I'm going!")
I'm going
I'm going
I'm going
.
.
I'm going
Let's add one line. What does this print out? →
keep_on_going = True
while keep_on_going:
print("I'm going!")
keep_on_going = False
I'm going
Going through each iteration
keep_on_going = True
while keep_on_going:
print("I'm going!")
keep_on_going = False
Loop ends after one iteration.
To change the outcome of your conditional:
Before you write your while loop, you should probably first determine…
How would you implement this?→
count = 2
while count <= 8:
print(count)
count = count + 2
count = 2
while count <= 8:
print(count)
count = count + 2
Write a program that… →
There are a few ways to do this! What are some general strategies for solving this problem?→
n = 1
while n <= 99:
if n != 13:
print(n)
n = n + 2
Using modulo to determine odds
n = 1
while n <= 99:
if n % 2 == 1 and n != 13:
print(n)
n = n + 1
Repeatedy ask if user wants cake until user says yes or yeah. How would you implement this?→
Do you want cake?
> no
Do you want cake?
> No
Do you want cake?
> yeah
Have some cake!
answer = 'no'
while answer != 'yes' and answer != 'yeah':
answer = input("Do you want cake?\n> ")
print("Have some cake!")
Let's make an assumption that the user enters "no" first, and then "yeah" second.
answer = 'no'
while answer != 'yes' and answer != 'yeah':
answer = input("Do you want cake?\n> ")
print("Have some cake!")
Write a program that will: →
Give me a number to add
> 10
Current total is 10
Give me a number to add
> 15
Current total is 25
Give me a number to add
> 5
Current total is 30
Give me a number to add
>
total = 0
while True:
n = int(input("Give me a number to add\n> "))
total = total + n
print("Current total is " + str(total))
Write a program that continually asks the user for numbers, and asks them if they'd like to keep going. In the end, it should output the average of all of the numbers entered→
I'll calculate the average for you!
Current total: 0
Numbers summed: 0
Please enter a number to add
> 10
Do you want to continue adding numbers (yes/no)?
> yes
Current total: 10
Numbers summed: 1
Please enter a number to add
> 12
Do you want to continue adding numbers (yes/no)?
> no
The average is 11.0
Let's try keeping track of multiple variables:
total = 0
count = 0
answer = 'yes'
print("I'll calculate the average for you!")
while answer == 'yes':
print("Current total: " + str(total))
print("Numbers summed: " + str(count))
total = total + int(input("Please enter a number to add\n> "))
count = count + 1
answer = input("Do you want to continue adding numbers (yes/no)?\n> ")
print("The average is "+ str(total / count))
We've used the following syntax to increment or decrement a variable
n = 0
n = n + 1
n = 100
n = n - 1
Slightly tedious…
There's some syntactic sugar that makes doing this less verbose: use += or -=
n = 0
# adds one to n and binds the resulting value to n
n += 1
n = 100
# subtracts one to n and binds the resulting value to n
n -= 1
This works for other operators too. What does this code print out? →
n = 2
n *= 2
n *= 2
print(n)
n = 64
n /= 2
n /= 2
print(n)
8
16.0
Also works with strings…. What does this code print out? →
s = "h"
s += "e"
s += "y"
s *= 3
print(s)
heyheyhey