Python

Programming

Lesson 9 - Iteration

Task 1 - Name Repetition

You need to create a program that will repeatedly display your name for as many times as your age using the print command. E.g. If you are 10 it should say it ten times. An example of how the program should work is shown to the right.

Use the success criteria below to help you:

  • Use input to ask the user to “Enter your name” and store it in a variable called name

  • Use input to ask the user to “Enter your age” and store it in a variable called age

  • Create a for loop using the code below:

for x in range(0, int(age)):

NOTE: the age variable is the top boundary of the loop, but it has been converted to an int

  • Use the print command inside the loop (make sure it is indented) so it will display “Your name is” and the name entered, like below:

Extension

Get the print message to also include the number before the message each time it adds it to the list box, an example is shown to the right.

How to do this:

  • x is a variable; therefore, you should add the x variable before “Your name is”.

REMEMBER: you need to convert x to a string as it is a number, therefore str(x)

  • x starts at 0 so think about how you can get it to start at 1 when put in the message.

IMPORTANT: You should not change what it starts at in the loop.

Task 2 - Timestable Program

You need to create a program that will help someone learn their times tables. They should enter a number between 1 and 12 and then it should display the times tables for that number between 0 and 12. An example of how the program should work is shown to the right.

Use the success criteria below to help you:

  • Use input to ask the user to “Enter the timestable you want to learn (between 1 and 12)” and store it in a variable called number

  • A variable called total created and set to 0

  • There should be a loop that is repeated 13 times

  • Each cycle through the loop it should:

    • Create a calculation to work out the total, this should be whatever was entered with the input and stored in number multiplied by x

    • The answer should be displayed using the print command saying the number entered multiplied by the count equals the total, like shown below:

REMEMBER: The structure for a FOR loop is:

for x in range(0,10):

CODE TO LOOP

Evidence point

Open the Assignment in Google Classroom called Student Evidence - L9 - Iteration.

Take a screenshot of your code for:

  • Task 1 - Name Repetition

  • Task 2 - Timestable Program

For each program click on Share and choose Link. Copy the link and paste it into the document, this means you can come back to it in future lessons.