Teacher code

Teacher code

A timetable friendly name.

Try


Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.

Watch this video to learn about the new concepts shown in the program:

Investigate


Questions to think about with this program to check your understanding:

Purpose question

What is the purpose of line 9:

space = teacher.find(” “)

REVEAL ANSWER

To find the index of where the space is in the teacher string. You need to know where the space is so that you can extract the next two letters.

Reason question

What is the reason for using `space + 1` and `space + 3` in line 10?

REVEAL ANSWER

`space + 1` is the index of the first character after the space. `space + 3` means up to two more characters after the space. The last index is always one more than you think it should be.

Make


Success Criteria

Change the program so that:

  1. The user can enter the number of letters to output.
  2. An apostrophe in the name is removed.

Typical inputs and outputs from the program would be:

Enter the name of the teacher: john smith

How many letters: 3

JSM


Enter the name of the teacher: john smith

How many letters: 5

JSMIT


Enter the name of the teacher: Dave O’Loughlin

How many letters: 4

DOLO

Knowledge Organiser

Use these resources to learn about new commands for this level and to help you meet the success criteria.

Notes:

Strings are zero indexed arrays of characters. Therefore you can index them like this:


word = “Hello”

print(word[1])


This code would output the letter “e”. The number 1 can be replaced with a variable. Be careful not to reference an index longer than the number of characters in the string minus one.


x = y.find(z)

x is assigned the index of the first instance of string z in string y. Returns -1 if the string is not found. Alternatively you can use `x = y.index(z)` but this will return an error if the string is not found.`


w = x[y: z]

w is assigned to be the substring from index y to index z – 1 from string x. E.g. `”Hello”[2:5]` is “llo”. This is useful for extracting a string from the beginning or middle of another string.


x = y[-z:]

x is assigned to be the substring in string y starting at the end and working backwards z characters. This is useful for extracting a string from the end of another string.


w = x.replace(y, z)

w is assigned to be the string x with all instances of string y replaced with string z.


x = y.join(z)

x is assigned to be a string concatenated from all elements of list z separated by string y.


w = x.split(y)

w is a new list from string x separated by character y.


x = y.strip(z)

x is assigned to be string y with all leading and trailing optional characters z removed.

x = x.strip()

Would remove any white space or escape characters at the beginning or end of string x.


x = y * z

x is assigned to be the string y, z times. E.g.

x = “@” * 5

Would result in x = “@@@@@”

Evaluate


Run the unit tests below to check that your program has met the success criteria.

Enter the name of the teacher: Craig Sargent

How many letters: 3

CSA

Enter the name of the teacher: Craig Sargent

How many letters: 5

CSARG

Enter the name of the teacher: Dave Hillyard

How many letters: 2

DH

Enter the name of the teacher: Dave Hillyard

How many letters: 4

DHIL

Enter the name of the teacher: Sam O’Toole

How many letters: 3

SOT

Craig'n'Dave logo

Craig ‘n’ Dave

In partnership with

Mission Encodeable
Bett Awards 2024 Finalist