Looping, Data Structures, and Functions
Activity 1: Review Week 2 Session 1
Decision making in a program
Activity 2: Demo how to create a guessing game Challenge that uses Looping
Identify sections of the Adventure Game framework that loop. Show
how the loop is terminated
Activity 3: Make a Flow chart
Flow charts are used when designing code
Activity 4: Write code for loops
Code sample for while loop
Break out of a while loop early
An infinite while loop - it runs forever - it has nothing to stop it
Exercise:
Write a flow chart for a for loop
Describe the difference between a for loop and a while loop
Activity 5: Python Data Structures
List: an ordered structure
List: can be accessed by number and numbers start at 0
What animal get's printed with this instruction? What do "square brackets" do?
Dictionary: "key:value"
Data is created by enclosing in "curly braces" and accessed by "square brackets"
You can loop through all the values in a dictionary but you can't count on the order
You can create a list of dictionary items, Is it OK to use single quotes or double quotes when creating dictionaries?
Why do I need 2 "for" loops to do the following? What is the output?
Activity 6: Functions
Functions help to organize code and prevent duplicating code through the program. A function is a named chunk of code that can be called from another section of the program. It can define parameters which are variables that will be passed to it when called. Functions can also return values.
We'll do exercises in which we write and call functions withparametersandreturnvalues. For example we can write a function that takes two numbers, adds them together and returns the sum. We'll also incorporate functions into the code we write for the Challenge.
Exercise:
Create and call a function that has no parameters and returns nothing
Create and call a function that has parameters and returns a value
Create a function that:
accepts a number number as a parameter
uses the parameter to initialize a loop
return the value of the number at the end of the loop
Activity 6: Write a Challenge for the game
For example
Activity 7: Write a Challenge for the Game that uses Looping and Functions