Learning Intentions
TBAT - explain what iteration is and some of the ways it can be used when writing a program
Success Criteria
Write code that includes iteration and add comments to explain how it works within the given program
TBAT - explain what iteration is and some of the ways it can be used when writing a program
Success Criteria
Write code that includes iteration and add comments to explain how it works within the given program
TASK:
The second line just prints out a message
What it’s doing is declaring a variable called x then putting the number 1 in it. Every time
the instruction on line 2 is executed the program jumps back to line 1 and increments x. Eventually x will be equal to 10 and the instruction on line 2 will stop being executed.
Well done!
Iteration is used to repeat an instruction. For loops are used to repeat an instruction a set number of times.
the instruction on line 2 is executed the program jumps back to line 1 and increments x. Eventually x will be equal to 10 and the instruction on line 2 will stop being executed.
Well done!
Iteration is used to repeat an instruction. For loops are used to repeat an instruction a set number of times.
The first line should be easy to understand. It declares a variable called intMyNum and puts the number 1 in it.
The second line is the clever one. It tells the computer to execute the following instructions only if the value in intMyNum is less than 10.
Line three prints out a message telling you the current value of intMyNum.
Notice the str() around the variable name? We need this because the value in intMyNum is an Integer data type but python can only print out String data types. The Str() command is converting the Integer in intMyNum to a String so it can be displayed.
Line four is incrementing the value in intMyNum (adding one to it). If we don’t do this then intMyNum will remain 1 forever and the loop will never end (because it will always be less than 10).
When line 4 has been executed the computer jumps back up to line 2, checks that the value in intMyNum is still less than 10 and if it is it executes the next instruction again.
The second line is the clever one. It tells the computer to execute the following instructions only if the value in intMyNum is less than 10.
Line three prints out a message telling you the current value of intMyNum.
Notice the str() around the variable name? We need this because the value in intMyNum is an Integer data type but python can only print out String data types. The Str() command is converting the Integer in intMyNum to a String so it can be displayed.
Line four is incrementing the value in intMyNum (adding one to it). If we don’t do this then intMyNum will remain 1 forever and the loop will never end (because it will always be less than 10).
When line 4 has been executed the computer jumps back up to line 2, checks that the value in intMyNum is still less than 10 and if it is it executes the next instruction again.
Question - Why does the program stop printing numbers after 9 not 10?
Data Structures
Line 1 declares an array called strMyArray and puts five names in to it. Each element in the array is separated by a comma.
The second line is printing out the value held in the second element of the array.
Question - Why does the program print the name Dave not the name Ted?
The second line is printing out the value held in the second element of the array.
Question - Why does the program print the name Dave not the name Ted?
Summary
What do you think this program will do?.......Find out!
Up for a challenge???
Challenge 1: Create a simple log in program that asks for a username and password. The program should let the user in if they get the username and password right but lock them out after three incorrect guesses.
Challenge 2: Create a simple chat bot that asks you questions and gives you feedback according to your answers.
Challenge 3: Create a Magic 8 Ball style program that generates a random answer when a user asks it a question.
Challenge 1: Create a simple log in program that asks for a username and password. The program should let the user in if they get the username and password right but lock them out after three incorrect guesses.
Challenge 2: Create a simple chat bot that asks you questions and gives you feedback according to your answers.
Challenge 3: Create a Magic 8 Ball style program that generates a random answer when a user asks it a question.