mastering python lesson3c_starry_universe_loops
TRANSCRIPT
www.teachingcomputing.com
Mastering Programming in Python
100s of other resources are available from
Lesson 3(c)FOR LOOPS CHALLENGES: Creating a universe with stars and planets
What can you do? Testing (what is it and the types of testing)
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
• Introduction to the language, SEQUENCE variables, create a Chat bot• Introduction SELECTION (if else statements)• Introducing ITERATION (While loops)• Introducing For Loops• Use of Functions/Modular Programming• Introducing Lists /Operations/List comprehension• Use of Dictionaries• String Manipulation• File Handling – Reading and writing to CSV Files• Importing and Exporting Files• Transversing, Enumeration, Zip Merging• Recursion• Practical Programming• Consolidation of all your skills – useful resources• Includes Computer Science theory and Exciting themes for everylesson including: Quantum Computing, History of Computing, Future of storage, Brain Processing, and more …
Series Overview
*Please note that each lesson is not bound to a specific time (so it can be taken at your own pace)
Information/Theory/Discuss
Task (Code provided)
Challenge (DIY!)
Suggested Project/HW
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 can’t 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 intelligentDesign. What do you think?
Scenario: God is looking to recruit a galaxy designer! He’ll hire the one who can solve the challenges to follow!
So, to get you to REALLY master loops, you’re going to have to work hard at solving some problems.
Problem solving isn’t 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, God’s keeping it simple and wants to create a universe with planets and stars.
Before he hires his top designer, you’ll 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
for <variable> in <sequence>:<statements>
else:<statements>
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:
Consider Minecraft. Everything in that universe is created from blocks!
You’ve 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 we’re looking for!
Things to remember #1Print() 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.
OUTPUT
• Wherever possible you have to try and solve these problems using FOR LOOPS (that’s 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!
Challenges coming up – the rules
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)
? OUTPUT
Screenshot your solution in the space above!
Desired output
Solution1:
?
Challenge 2: Create a universe with one line of 10 stars and a line to follow of 10 planets (numbers 0 to 9)
? OUTPUT
Screenshot your solution in the space above!
Desired output
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)
?Screenshot your solution in the space above!
Desired output
This isn’t quite what we want!Type out the code below. It doesn’t quite produce
our desired output. Change it so it does!
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 Loops3. A more elegant solution: Using just 2 loops (nested for loop)*Make sure your variable names are sensible. If you weren’t usingJust 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)
Challenge 7: Create a planetary arrangement as shown below (triangular arrangement of numbers)
?Screenshot your solution in the space above!
Desired output
Difficult!
Solution 7: (showing a build up to a solution)
Challenge 8: Correct the code to get it to produce the desired solution (user specified box of stars)
?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?
Type out the code below. It doesn’t quite produce our desired output. Change it so it does!
Difficult!
Solution 8:
Discussion: Astronomy and Coding
https://en.wikipedia.org/wiki/Digital_physics
You may find Wikipedia’s listing on digital universe possibilities an interesting read
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 is therefore computable.
Some scientists say that the universe may in fact be a computer program.
Testing a Program
Remember the Systems Life Cycle?STAGES:Identify the Problem
Analysis
Design
Implementation
TestingEvaluation
Maintenance (this is on-going maintenance)
‘Testing’ - an essential stage in development.
• 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!
What is Testing?
• How many of these types of testing look familiar?
Wikipedia entry on Testing
• Test data is data which has been specifically identified for use in tests, typically of a computer program.
+ =1. This program is supposed to add two inputs between 1 & 1002. You could test it with TEST DATA “3” and “6”3. The expected output for the test data provided is “9”
The adding app
Test Data: Data you use to test your program
• 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?
+ =The adding app
Using the table below, give 3 separate test cases for testing the program
Input Data Reason for Test
Expected Result
Task
Input Data Reason for Test Expected Result
2,3 Normal Data Test (can the program handle normal data)
5
2.4, 2.1 Real Numbers – can it handle?
4.6
0,100 Borderline Values 100
-1, 101 Invalid Values
+ =The adding app Enter any two
numbers between 1- 100 and it will
be added for you!
Task
Using the table below, give 3 separate test cases for testing the program
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)
5
2.4, 2.1 Real Numbers – can it handle?
4.6
0,100 Borderline Values 100
-1, 101 Invalid Values
+ =The adding app Enter any two
numbers between 1- 100 and it will
be added for you!
Answers
Input Data Reason for Test Expected Result
2,3 Normal Data Test (can the program handle normal data)
5
2.4, 2.1 Real Numbers – can it handle?
4.6
0,100 Borderline Values 100
-1, 101 Invalid Values
+ =The adding app Enter any two
numbers between 1- 100 and it will
be added for you!
Answers
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)
5
2.4, 2.1 Real Numbers – can it handle?
4.6
0,100 Borderline Values 100
-1, 101 Invalid Values
+ =The adding app Enter any two
numbers between 1- 100 and it will
be added for you!
Answers
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)
5
2.4, 2.1 Real Numbers – can it handle?
4.6
0,100 Borderline Values 100
-1, 101 Invalid Values
+ =The adding app Enter any two
numbers between 1- 100 and it will
be added for you!
Answers
Using the table below, give 3 separate test cases for testing the program
1.Normal Data2.Extreme Data
3.Boundary or Borderline Data4.Erroneous or Invalid Data
Different types of Test Data
•White Box testing•Black Box Testing
•Alpha Testing•Beta Testing
•Acceptance Testing
Other types 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.
What type of testing?
Task: 10 minutes to research the types of testingWhite Box * Black Box * Alpha Testing * Beta Testing * Acceptance Testing
?
?
?
Griffin does a demonstration to the end user to show all parts of the program work correctly before handover. 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.
What type of testing?
Task: 10 minutes to research the types of testingWhite Box * Black Box * Alpha Testing * Beta Testing * Acceptance Testing
Acceptance
Black Box
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