Professional Documents
Culture Documents
4-Mastering Python Lesson4 For Loops
4-Mastering Python Lesson4 For Loops
Lesson 4
The full series and 100s of other resources are available from
www.teachingcomputing.com
Series Overview
Information/Theory/Discuss
Task (Code provided)
Challenge (DIY!)
Suggested Project/HW
*Please note that each lesson is not bound to a specific time (so it can be taken at your own pace)
Answers
:
Nested Loops
A Nested loop is basically a loop within a loop. You can have a for
loop inside a while loop or vice versa. Lets start with something
simple. Use a for loop to print out the items in a list.
OUTPU
T
And now for a Nested Loop. This will break each word into its
constituent letters and print them out.
OUTPU
T
Output
3
3
Output
3
4
Note the
indentation!
This second for
loop is nested
INSIDE the first
one!
0
1
2
3
4
5
6
i
0
1
2
Output
0
1
2
7
8
9
0
1
2
10
11
12
0
1
2
12
Desired output
OUTPU
T
Note only 1,2,3,4 are printed from the range and once x = 5, the loop is
exited!
Nothing!
???????
This is
because the loop finds
OUTPUT?
1 and breaks before it
can do anything!
Answers
:
totalmerits= 0
for i in range(5):
name = str(input("Enter
Student Name from the 'Red'
House: "))
new_number = int(input("Enter
a number: " ))
totalmerits += new_number
#------------------------------------------------------------------------------# Name:
Maths Quiz
# Purpose: Tutorial
# Author:
teachingcomputing.com
# Created:
23/02/2016
# Copyright: (c) teachingcomputing.com 2016
#------------------------------------------------------------------------------import random
#identifying the variables we will be using
score=0
answer=0
operators=("x", "+", "-")
#Randomly generate the numbers and operators we will be
https://youtu.be/atMuFCpxnUQ
https://youtu.be/9LgyKiq_hU0
Konrad Zuse