replit

Before you start this course, you must login to Replit

If you have forgotten your Replit account user name or password, ask your Coder Coach.

CoderSports


12.5 – Predict & Run

At the ‘predict’ & ‘run’ stages students work entirely with example code. They should inspect it carefully and write a prediction about what it will do.

They then run the code and compare the result to their prediction.


In this unit you will learn about: Lists

It is possible to output some consecutive items from a list by customising the print function to add a start and end index for the output.

There are a couple of traps to be careful of here.

The start value IS included in the output.

The end value IS NOT included in the output.

CODE EXAMPLES

# Create and populate a list
countries = ["UK", "USA", "Chad", "Australia", "Thailand"]

# Outputs 'USA', 'Chad', 'Australia'

print(countries[1:4])

# Outputs from the beginning of the list to 'Chad'.

print(countries[:3])

# Outputs from 'USA' to the end of the list.

print(countries[1:])

Help! My Code Does Not Work!

  • Square brackets for items in the list.
  • Commas between each item in the list.
  • String items are in quotation marks.
  • Range – the end value is NOT included in the output.
  • Search – not initialising counter OUTSIDE the loop.
  • Search – not incrementing counter INSIDE the loop.

Hints