Вы находитесь на странице: 1из 4

Name:

All the fun of the fair

Snakeworld is a new theme park opening up in the North West of England


and they want you to write some programs using Python to help with the
running of the park. They have four types of rides: Roller coasters
(named after snakes), Water Rides, Childrens Rides (for children under
10) and Family Rides.

Task 1 Using Strings


A string in a program is a block of text which can include letters, numbers
or symbols.
In a new program window, create a string to include all the names of the
rollercoaster rides using the code below.
rollercoasters=python,cobra,viper,copperhead
waterRides=logflume,rattlesnakerapids,lazyriver
familyRides=twister,bigwheel,ghosttrain
childRides=babysnake,snakejungle,spinningteapots
allRides=rollercoasters+waterRides+familyRides+childRides
printallRides

Save the program and run it in the python shell window

Name:
What does the line printallRides do?

What does the line allRides=rollercoaster+waterRides+


familyRides+childRides do?

Are there any problems with the allRides variable?

Task 2 - Lists
It is often more useful to store information as a list instead of a string.
This is similar to an array in other programming languages.
Write a new program in a new window which stores each type of ride in a
list
rollercoasters=['python','cobra','viper','copperhead']
familyRides=['twister','bigwheel','ghosttrain']
waterRides=['logflume','rattlesnakerapids','lazyriver']
childRides=['babysnake','snakejungle','spinningteapots']

allRides=rollercoasters+waterRides+familyRides+childRides
printallRides

What is printed on screen this time when you ran the code?

What happens if you type print allRides[0] into the python shell?

What happens when you type print allRides[4:6] into the python
shell?

What would you type into python to print out just a list of the family
rides?

Name:
Check it works
Task 3 - Dictionaries/Maps
Another data type in Python is a Dictionary (sometimes called a Map).
This is similar to a list but has a key which can be used to find the
information quickly.
Find out which ride six people in the class prefer and enter them into the
table below.
Name

Favourite Type of ride


(Rollercoaster, Water ride, Family
ride)

In Python {} are used to create a Dictionary. Each element has a key and
value.
favouriteRide={Matthew:Water,Jenny:Family,Cara:Rollercoaster,
Jimmy:Rollercoaster}

Open a new window and create a Dictionary to store the favourite rides
for the pupils that you entered into the table above.
Write a command to print out your dictionary
What do you notice about what is printed on screen?
Write a command to print out just one persons favourite ride (Hint print
favouriteRide[name])

Name:

Extension Lists within lists


You can store different data types in lists such as strings or integers. You
can even store lists within a list.
In a new program window, create a list called favouriteThemePark which
stores the name of a pupil along with their favourite theme park and their
favourite ride at the theme park.

Copy and paste your code into here

Вам также может понравиться