Sie sind auf Seite 1von 45

Mastering Programming in Python

Lesson 5

FOR LOOPS CHALLENGES: Creating a universe with stars and planets


What can you do? Testing (what is it and the types of testing)

100s of other resources are available from

www.teachingcomputing.com

Series Overview

Introduction to the language, SEQUENCE variables, create a Chat bot


SELECTION (if else statements)
ITERATION (While loops)
Introducing For Loops
Challenges and tasks for Mastering Iteration
Use of Functions/Modular Programming
Introducing Lists /Operations/List comprehension
Tuples, sets, lists, dictionaries
Use of Dictionaries
Doing things with strings
File Handling Reading, writing, appending text files
Working with CSV files
Practical Programming (plenty of engaging scenarios, challenges
and tasks to get you thinking)
Consolidation of all your skills useful resources
Includes Computer Science theory and Exciting themes for every
lesson including: Quantum Computing, History of Computing,
Future of storage, Brain Processing, Systems life Cycle, Testing
and more

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)

In this lesson you will


MORE ON LOOPS (ITERATION) .
*As Loops are so integral to programming, you really
do need to get good at coding them! Practice makes
perfect.
Attempt to solve a series of challenges
involving for loops and nested loops
The Print() command
The end statement and how you can get it to
work for you!
Learn about how to test a program valid,
invalid and boundary data

What do we know about our universe?


Dark matter is a hypothetical substance that is believed by most astronomers to
account for around five-sixths of the matter in the universe. We cant see it, yet there is
more dark matter than there is observable matter!
The universe is full of mysteries. What we do
know however, is that when we look up at
the sky we see what we believe to be
galaxies.
The shapes of spiral galaxies, such as
Messier 74, and hurricanes, such as
Hurricane Irene, follow the Fibonacci
sequence.
This and other evidence of codes and patterns in nature seems to point to intelligent
Design. What do you think?

Scenario: God is looking to recruit a galaxy designer!


Hell hire the one who can solve the challenges to follow!
So, to get you to REALLY master loops, youre going to
have to work hard at solving some problems.
Problem solving isnt always easy, but it is worth it in the
end. Remembers the TIME you spend working on a
problem is valuable. Try not to give up and look at the
answers.
In this scenario, Gods keeping it simple and wants to
create a universe with planets and stars.
Before he hires his top designer, youll have to prove you can scope out and solve a
few problems (that involve creating planets and stars) All you need to know is that:
Stars will be represented by * (asterix) and Planets = (numbers) 0,1,2,3,4,5 etc.

Recap: The anatomy of a For Loop


The For Loop can step through the items in any
ordered sequence list, i.e. string, lists, tuples, the
keys of dictionaries and other iterables. It starts
with the keyword "for" followed by an arbitrary
variable name. This will hold the values of the
following sequence object, which is stepped
through. Generally, the syntax looks like this:

for <variable> in <sequence>:


<statements>
else:
<statements>

Consider Minecraft. Everything in that


universe is created from blocks!
Youve got to start somewhere! In Minecraft it would be about starting with a single
block. In our scenario, we could start by printing a single star!

But what if we wanted a 100 stars? (or


even 10?) You could type out the
above 10 times, but a for loop is what
were looking for!

Things to remember #1
Print() is quite useful if you want to print output on the next line!

OUTPUT

Things to remember #2
'end' is the special character at the end of the print statement,
which is by default the newline '\n. It can be used to insert a
character of your choice too.

OUTPU
T

Challenges coming up the rules


Wherever possible you have to try and solve these problems using FOR
LOOPS (thats our focus for this session).
Break the problem down if it helps.

Solve it sequentially, and then think about what you need to do to use a loop to
do the same thing!
Avoid While Loops for these set of challenges.
Feel free to refer to previous power points and slides in this presentation!
Note: The challenges get harder as they go on. If you get to the end
and solve them yourself .
VERY WELL DONE!

Challenge 1: Create a universe with two lots


of 10 stars (one after the other)

?
Screenshot your solution in the space above!

Desired output

OUTPU
T

Solution1:

Challenge 2: Create a universe with one line of 10


stars and a line to follow of 10 planets (numbers 0 to 9)

?
Screenshot your solution in the space above!

Desired output

OUTPU
T

Solution 2:

Note the i here


that is responsible
for printing out
every number in
the range specified
(0 to 9)

Challenge 3: Correct the code to get it to produce the


desired solution (10 x 10 matrix of stars)
Type out the code below. It doesnt quite produce
our desired output. Change it so it does!

?
Screenshot your solution in the space above!

This isnt quite what we want!

Desired output

Solution 3:

Challenge 4: Create a matrix (3 x 20) of stars using a


nested for loop. (for loop within a for loop)

?
Screenshot your solution in the space above!

Desired output

Solution 4:

Challenge 5: Now for planets (which are numbers).


Create a matrix (5 x 5) of planets (numbers) with similar
planets on the same column (see below)

?
Screenshot your solution in the space above!

Desired output

Hint: See if you can produce this solution in different ways.


1. Simple sequence (using just the print() statement)
2. Using multiple for Loops
3. A more elegant solution: Using just 2 loops (nested for loop)
*Make sure your variable names are sensible. If you werent using
Just letters, what might be sensible identifiers to use?

Solution 5 (different solutions):

Challenge 6: Create a similar matrix of planets (5 x 5)


but this time similar planets(numbers) on the same row.

?
Screenshot your solution in the space above!

Desired output

Solution 6: (showing a build up to a solution)

Difficult!

Challenge 7: Create a planetary arrangement as


shown below (triangular arrangement of numbers)

?
Screenshot your solution in the space above!

Desired output

Solution 7: (showing a build up to a solution)

Difficult!

Challenge 8: Correct the code to get it to produce the


desired solution (user specified box of stars)
Type out the code below. It doesnt quite produce
our desired output. Change it so it does!

?
Screenshot your solution in the space above!

Desired output: When the user enters a size, the


program creates a box of that size (of stars). What
do you need to change to get it to work?

Solution 8:

Discussion: Astronomy and Coding


You may find Wikipedias listing on digital universe possibilities an
interesting
read
https://en.wikipedia.org/wiki/Digital_physics
In physics and
cosmology, digital
physics is a
collection of
theoretical
perspectives based
on the premise that
the universe is, at
heart, describable
by information and
Some scientists say that the universe may in fact be a computer
is therefore

Testing a Program

Testing - an essential stage in development.


Remember the Systems Life Cycle?
STAGES:
Identify the Problem
Analysis
Design
Implementation

Testing
Evaluation

What is Testing?
Testing is a stage of the systems life cycle which is essential in the development
process.

Testing ensures that the program/system works as it should.

Testing also eliminates the possibility of any bugs or errors that may emerge on
use of the system/program

There are various methods of testing and different stages of testing too!

Wikipedia entry on Testing


How many of
these types of
testing look
familiar?

Test Data: Data you use to test your program


Test data is data which has been specifically identified for use in
tests, typically of a computer program.

The adding app

1. This program is supposed to add two inputs between 1 & 100


2. You could test it with TEST DATA 3 and 6
3. The expected output for the test data provided is 9

The adding app

In testing the above application, the tester decided to test the


addition module using groups of four pairs of input at a time.
Why do you think it is best to test it with only four tests pairs
initially?

k
s
a
T
Using the table below, give 3 separate test cases for testing
the program
Input Data

Reason for
Test

Expected
Result

k
s
a
T

Enter any two


numbers
between 1100 and it will
be added for
you!

The adding app

Using the table below, give 3 separate test cases for testing the program

Input Data

Reason for Test

Expected Result

2,3

Normal Data
Test (can the
program handle
normal data)
Real Numbers
can it handle?
Borderline
Values
Invalid Values

2.4, 2.1
0,100
-1, 101

4.6
100

Ans

s
wer

Enter any two


numbers
between 1100 and it will
be added for
you!

The adding app

Using the table below, give 3 separate test cases for testing the program

Input Data

Reason for Test

Expected Result

2,3

Normal Data
Test (can the
program handle
normal data)
Real Numbers
can it handle?
Borderline
Values
Invalid Values

2.4, 2.1
0,100
-1, 101

4.6
100

Ans

s
wer

Enter any two


numbers
between 1100 and it will
be added for
you!

The adding app

Using the table below, give 3 separate test cases for testing the program

Input Data

Reason for Test

Expected Result

2,3

Normal Data
Test (can the
program handle
normal data)
Real Numbers
can it handle?
Borderline
Values
Invalid Values

2.4, 2.1
0,100
-1, 101

4.6
100

Ans

s
wer

Enter any two


numbers
between 1100 and it will
be added for
you!

The adding app

Using the table below, give 3 separate test cases for testing the program

Input Data

Reason for Test

Expected Result

2,3

Normal Data
Test (can the
program handle
normal data)
Real Numbers
can it handle?
Borderline
Values
Invalid Values

2.4, 2.1
0,100
-1, 101

4.6
100

Ans

s
wer

Enter any two


numbers
between 1100 and it will
be added for
you!

The adding app

Using the table below, give 3 separate test cases for testing the program

Input Data

Reason for Test

Expected Result

2,3

Normal Data
Test (can the
program handle
normal data)
Real Numbers
can it handle?
Borderline
Values
Invalid Values

2.4, 2.1
0,100
-1, 101

4.6
100

Different types of Test Data

1. Normal Data
2. Extreme Data
3. Boundary or Borderline Data
4. Erroneous or Invalid Data

Other types of Testing

White Box testing


Black Box Testing
Alpha Testing
Beta Testing
Acceptance Testing

Task: 10 minutes to research the types of testing


White Box * Black Box * Alpha Testing * Beta Testing * Acceptance Testing

What type of testing?

Griffin does a demonstration to the


end user to show all parts of the
program work correctly.

Melvin uses Predefined test data


that is input and the output is
compared with the expected results.

Matt gives the program to a few


third-party testers to use and report
any errors or bugs.

Task: 10 minutes to research the types of testing


White Box * Black Box * Alpha Testing * Beta Testing * Acceptance Testing

What type of testing?

Griffin does a demonstration to the


end user to show all parts of the
program work correctly before
handover.

Acceptance

Melvin uses Predefined test data


that is input and the output is
compared with the expected results.

Black Box

Matt gives the program to a few


third-party testers to use and report
any errors or bugs.

Beta Tests

Suggested Project / HW / Research


Create a information power point on 5
different programming languages.
(There are literally hundreds out there.
Examples: Java, Python, Lua, VB.Net,
Pascal)
Include the following in your research:
Main features of the programming
languages. Click here for a list on
Wikipedia!
Advantages and Disadvantages
Popularity and usage across the world /
for what applications/systems
Fibonacci sequence written in these 5
languages. Analyse the differences in
code

Useful links and additional reading


https://wiki.python.org/moin/ForLoop
http://www.tutorialspoint.com/python/python_for_loop.htm
http://www.learnpython.org/en/Loops
https://en.wikipedia.org/wiki/List_of_programming_languages
https://en.wikipedia.org/wiki/Konrad_Zuse
http://www.victorianweb.org/science/science_texts/bridgewater/intro.htm

Das könnte Ihnen auch gefallen