variables, references and data structures

126
COMP 1001: Introduction to Computers for Arts and Social Sciences Making Stories Interactive Variables, References and Data Structures

Upload: others

Post on 03-Jun-2022

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Making Stories Interactive

Variables, References and Data Structures

Page 2: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Learning Objectives

• Learn how information is stored on a computer.

• Learn basic programming concepts (variables, if statements, loops, and

functions) and write simple programs using these concepts.

Page 3: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Page 4: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

What is a Story?

Page 5: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

ACTORS perform actions

THE WORLD changes

EVENTS relate to each other

Page 6: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

About people, not things STORIES:

conflict

Page 7: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Interactivity

Page 8: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Two or more agents

Understand each other

Respond properly

Page 9: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Tools

Page 10: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

http://twinery.org/

Page 11: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

http://www.inklestudios.com/inklewriter/

Page 12: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

http://www.renpy.org/

Page 13: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

From the Beginning…

Page 14: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

We will examine how to do a simple text-based interactive story.

Page 15: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

User Input

We want to ask the user a question.

print("Choose from the following options:")

print("1: go left")

print("2: go right")

input("> ")

Page 16: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

User Input

We want to ask the user a question.

print("Choose from the following options:")

print("1: go left")

print("2: go right")

input("> ") This function returns an answer. Where should we put it?

Page 17: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Variables

A variable is like a box we can put information in.

Page 18: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

answer = input("> ")

answer

2

Variables

The user types 2 and presses enter.

Page 19: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

answer = input("> ")

answer

Variables

The value is stored in the box.

2

Page 20: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

if answer == 2:

print("You chose to go left, ")

print(" a most dangerous path.")

answer

2

Variables

Page 21: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

if answer == 2:

print("You chose to go left, ")

print(" a most dangerous path.")

answer

2

Variables

Look at what's inside the box and

do something with it

Page 22: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

x = 0

while x < 2:

x = x + 1

x

0

Variables

Page 23: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

x = 0

while x < 2:

x = x + 1

x

0

Variables

Put zero in a box labelled x.

Page 24: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

x = 0

while x < 2:

x = x + 1

x

0

Variables

Peek in the box to get the value…

Page 25: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

x = 0

while x < 2:

x = x + 1

x

0

Variables

…and compare it to 2

Page 26: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

x = 0

while x < 2:

x = x + 1

x

0

Variables

Peek in the box to get the value…

Page 27: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

x = 0

while x < 2:

x = x + 1

x

1

Variables

…add 1 to it…

Page 28: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

x = 0

while x < 2:

x = x + 1

x

1

Variables

…and put the result back into the box

Page 29: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

x = 0

while x < 2:

x = x + 1

x

Variables

Now the box contains the value 1.

1

Page 30: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

What is the value of y after the execution of this code? x = 37

y = x + 2

x = 20

Text 37607 1052115: 39 1052116: 22 1052117: 59 1052118: 20 1052119: 57

Page 31: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Data Types

We know there are different ways of representing data in memory using binary

numbers.

Page 32: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Data Types

Name Type

int plain integer – no decimal point

float floating point number – has a decimal point

complex real + imaginary parts

string text between quotes

Page 33: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Data Types

We know there are different ways of representing data in memory using binary

numbers.

How does our variable know what type of data it is storing?

Page 34: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Data Types

Answer: Context.

Variable Data Type

a = 3 integer

a = 3.4 float

a = "3.4" string

a = 0b11 integer

a = "0b11" string

Page 35: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Stories as Data

Page 36: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Storing Story Data We'll represent a short story based on the classic nursery

rhyme Little Miss Muffet:

Little Miss Muffet sat on her tuffet Eating her curds and whey When along came a spider And sat down beside her

And scared Miss Muffet away

http://crown-heart.deviantart.com/art/Little-Miss-Muffet-162099928

Page 37: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

1. The sun shone brightly when Miss Muffet finally arrived at her favorite tuffet. Though it seemed odd to others that she’d decide to store any furniture outdoors, Miss Muffet loved sitting on the footstool under her favorite tree.

2. Just like every morning, Miss Muffet was famished. Luckily, just like every morning, she had brought her curds and whey to enjoy for breakfast.

3. As Miss Muffet brought the first spoonful to her mouth, a very long-legged but otherwise friendly spider noticed her sitting below. The spider headed downward, released his thread above him, so he could visit the curious creature now seated under his tree.

4. As the spider landed on Miss Muffet’s spoon, the girl sensed something was wrong. As soon as she saw the insect, she screamed before she could even consider that she was, in fact, facing a friendly spider.

5. Miss Muffet was gone before the spider could even say hello. The spider felt terrible for scaring her away, but soon forgot as he investigated the curds and whey left behind.

Page 38: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

1. The sun shone brightly when Miss Muffet finally arrived at her favorite tuffet. Though it seemed odd to others that she’d decide to store any furniture outdoors, Miss Muffet loved sitting on the footstool under her favorite tree.

2. Just like every morning, Miss Muffet was famished. Luckily, just like every morning, she had brought her curds and whey to enjoy for breakfast.

3. As Miss Muffet brought the first spoonful to her mouth, a very long-legged but otherwise friendly spider noticed her sitting below. The spider headed downward, released his thread above him, so he could visit the curious creature now seated under his tree.

4. As the spider landed on Miss Muffet’s spoon, the girl sensed something was wrong. As soon as she saw the insect, she screamed before she could even consider that she was, in fact, facing a friendly spider.

5. Miss Muffet was gone before the spider could even say hello. The spider felt terrible for scaring her away, but soon forgot as he investigated the curds and whey left behind.

What data type do we need, and how will we store these values?

Page 39: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Data Structures

Used to organize information on a computer so it can be

used efficiently for the task at hand

Page 40: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

The List Data Structure

Page 41: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

The List Data Structure

These numbers are called indexes

Page 42: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

The List Data Structure

Computer scientists count from zero

Page 43: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Index Scene

0

The sun shone brightly when Miss Muffet finally arrived at her

favorite tuffet. Though it seemed odd to others that she’d decide

to store any furniture outdoors, Miss Muffet loved sitting on the

footstool under her favorite tree.

1

Just like every morning, Miss Muffet was famished. Luckily, just

like every morning, she had brought her curds and whey to enjoy

for breakfast.

2

As Miss Muffet brought the first spoonful to her mouth, a very

long-legged but otherwise friendly spider noticed her sitting

below. The spider headed downward, released his thread above

him, so he could visit the curious creature now seated under his

tree.

3

As the spider landed on Miss Muffet’s spoon, the girl sensed

something was wrong. As soon as she saw the insect, she

screamed before she could even consider that she was, in fact,

facing a friendly spider.

4

Miss Muffet was gone before the spider could even say hello. The

spider felt terrible for scaring her away, but soon forgot as he

investigated the curds and whey left behind.

Page 44: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Lists in Python

Creating a list:

[5, 3, -2, 10]

Page 45: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Lists in Python

Creating a list:

[1, -9.5555, "hello", [1, 2, 3]]

Page 46: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Lists in Python

Creating a list:

[1, -9.5555, "hello", [1, 2, 3]]

Lists can contain multiple data types – even other

lists!

Page 47: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Lists in Python

Assigning a list to a variable:

myList = [1, -9.5555, "hello", [1, 2, 3]]

Page 48: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Lists in Python

Accessing items in a list:

myList = [1, -9.5555, "hello", [1, 2, 3]]

print(myList[2])

Page 49: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Lists in Python

Accessing items in a list:

myList = [1, -9.5555, "hello", [1, 2, 3]]

print(myList[2])

Prints "hello"

Page 50: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Lists in Python

Getting the length of a list:

len(myList)

Page 51: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Lists in Python

Getting the length of a list:

print(myList[len(myList)-1])

Page 52: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Lists in Python

Getting the length of a list:

print(myList[len(myList)-1])

Prints the last item in the list (remember, starts at

zero!)

Page 53: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Lists in Python

Getting the length of a list:

print(myList[-1])

Shortcut for accessing last item in the list

Page 54: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

missMuffetScenes = [

"The sun shone brightly…",

"Just like every morning…",

"As Miss Muffet brought…",

"As the spider landed on…",

"Miss Muffet was gone…"

]

Scene text abbreviated for space…

Page 55: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Printing List Items

How can we print out our story scenes?

Page 56: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Printing List Items

print(missMuffetScenes[0])

print(missMuffetScenes[1])

print(missMuffetScenes[2])

print(missMuffetScenes[3])

print(missMuffetScenes[4])

Page 57: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Printing List Items

print(missMuffetScenes[0])

print(missMuffetScenes[1])

print(missMuffetScenes[2])

print(missMuffetScenes[3])

print(missMuffetScenes[4])

Is this really going to work for longer stories?

Page 58: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

While Loop

Drive the track while the race is not over

Page 59: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Printing List Items

currentIndex = 0

while currentIndex <= 4:

print(missMuffetScenes[currentIndex])

currentIndex = currentIndex + 1

Page 60: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Printing List Items

currentIndex = 0

while currentIndex <= 4:

print(missMuffetScenes[currentIndex])

currentIndex = currentIndex + 1

We want the first index to give us the first scene

in the list

Page 61: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Printing List Items

currentIndex = 0

while currentIndex <= 4:

print(missMuffetScenes[currentIndex])

currentIndex = currentIndex + 1

Drive laps so long as we haven't gone past the

last item

Page 62: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Printing List Items

currentIndex = 0

while currentIndex <= 4:

print(missMuffetScenes[currentIndex])

currentIndex = currentIndex + 1

Should use len(missMuffetScenes)-1

Page 63: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Printing List Items

currentIndex = 0

while currentIndex <= 4:

print(missMuffetScenes[currentIndex])

currentIndex = currentIndex + 1

Drive the lap is printing the scene…

Page 64: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Printing List Items

currentIndex = 0

while currentIndex <= 4:

print(missMuffetScenes[currentIndex])

currentIndex = currentIndex + 1

…and updating the index to get ready for the next lap

Page 65: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

For Loop

Drive the track one lap for every item in a list

Page 66: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

For Loop

for <variableName> in <listName>:

<code to run a number of times

equal to the size of the list>

Page 67: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Printing List Items

for scene in missMuffetScenes:

print(scene)

Page 68: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Printing List Items

for scene in missMuffetScenes:

print(scene)

The list that we will be looping over

Page 69: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Printing List Items

for scene in missMuffetScenes:

print(scene)

This variable will get a new value from the list every lap

Page 70: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Printing List Items

for scene in missMuffetScenes:

print(scene)

Driving the lap is just printing this time – everything else is

updated automatically

Page 71: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Things to Do With Scene Lists

Useful in video games, when players can explore the world in

between scenes.

Allows us to rearrange the ordering of the scenes.

Page 72: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Rearranging List Items

Lists can be rearranged with a series of swaps.

Page 73: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Rearranging List Items

Swapping the first and last items…

missMuffetScenes[-1] = missMuffetScenes[0]

missMuffetScenes[0] = missMuffetScenes[-1]

Page 74: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Rearranging List Items

Swapping the first and last items…

missMuffetScenes[-1] = missMuffetScenes[0]

missMuffetScenes[0] = missMuffetScenes[-1]

!!!

Page 75: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Rearranging List Items

missMuffetScenes[-1] = missMuffetScenes[0]

missMuffetScenes[0] = missMuffetScenes[-1]

Page 76: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Rearranging List Items

missMuffetScenes[-1] = missMuffetScenes[0]

missMuffetScenes[0] = missMuffetScenes[-1]

Get the contents of the first box…

Page 77: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Rearranging List Items

missMuffetScenes[-1] = missMuffetScenes[0]

missMuffetScenes[0] = missMuffetScenes[-1]

…and put them into the last box

Page 78: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Rearranging List Items

missMuffetScenes[-1] = missMuffetScenes[0]

missMuffetScenes[0] = missMuffetScenes[-1]

Now we're just copying scene1 back into the first box

Page 79: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Rearranging List Items

Swapping the first and last items…

temp = missMuffetScenes[-1]

missMuffetScenes[-1] = missMuffetScenes[0]

missMuffetScenes[0] = temp

Page 80: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Rearranging List Items

temp = missMuffetScenes[-1]

missMuffetScenes[-1] = missMuffetScenes[0]

missMuffetScenes[0] = temp

Page 81: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Rearranging List Items

temp = missMuffetScenes[-1]

missMuffetScenes[-1] = missMuffetScenes[0]

missMuffetScenes[0] = temp

First put scene3 into the temporary box

Page 82: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Rearranging List Items

temp = missMuffetScenes[-1]

missMuffetScenes[-1] = missMuffetScenes[0]

missMuffetScenes[0] = temp

Page 83: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Rearranging List Items

temp = missMuffetScenes[-1]

missMuffetScenes[-1] = missMuffetScenes[0]

missMuffetScenes[0] = temp

Then we can copy scene1 to the last box before copying scene3 into the first box (now

shown)

Page 84: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

What is the value of a after the execution of this code? a = [2, 4, 6, 8]

a.remove(4) #item

a.pop(2) #index

Text 37607 622977: [2,4] 622997: [6,8] 622998: [2,6] 622999: [2,8] 623000: Nothing / error

Page 85: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

What is printed by this code? lst = [3, 6, 9]

sum = 0

counter = 0

while counter < len(lst):

sum += counter

counter += 2

print(sum)

Text 37607 634005: 18 634007: 6 634014: 2 634015: 9 634021: Nothing / error

Page 86: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Branching Stories

Page 87: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Page 88: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

This is a tree data structure

Page 89: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

As the spider landed on Miss Muffet’s spoon, the girl sensed something was wrong. Miss Muffet should run away as soon as she sees the spider. Miss Muffet should stay to greet the spider.

Page 90: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

As the spider landed on Miss Muffet’s spoon, the girl sensed something was wrong. Miss Muffet should run away as soon as she sees the spider. Miss Muffet should stay to greet the spider.

Page 91: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

As the spider landed on Miss Muffet’s spoon, the girl sensed something was wrong. As soon as she saw the insect, she screamed before she could even consider that she was, in fact, facing a friendly spider. Miss Muffet was gone before the spider could even say hello. The spider felt terrible for scaring her away, but soon forgot as he investigated the curds and whey left behind.

The End

Page 92: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

As the spider landed on Miss Muffet’s spoon, the girl sensed something was wrong. Miss Muffet should run away as soon as she sees the spider. Miss Muffet should stay to greet the spider.

Page 93: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

When she saw the spider, curiosity got the best of her. “Why hello, there, little friend!” she said. Offer the spider some curds and whey. Don’t share with the spider.

Page 94: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

When she saw the spider, curiosity got the best of her. “Why hello, there, little friend!” she said. Offer the spider some curds and whey. Don’t share with the spider.

Page 95: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

When she saw the spider, curiosity got the best of her. “Why hello, there, little friend!” she said. Miss Muffet held out her spoon as the wide-eyed spider grinned and lunged for the delicious snack. From that day forward, the spider joined Miss Muffet every morning for breakfast with his new friend.

The end

Page 96: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

When she saw the spider, curiosity got the best of her. “Why hello, there, little friend!” she said. Offer the spider some curds and whey. Don’t share with the spider.

Page 97: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

When she saw the spider, curiosity got the best of her. “Why hello, there, little friend!” she said. Although Miss Muffet tried to make small talk with the spider, he was a little put off by her unwillingness to share. The spider decided he’d try another tree to see if he could find someone a little more generous with their breakfast. Miss Muffet never saw him again.

The end

Page 98: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Page 99: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

How can we represent a scene as data?

Page 100: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

How can we represent a scene as data?

We need: - The text of a scene - Scenes that either offer a choice or don't - For scenes that do offer a choice, the text

for the options - What scene to go to next (automatically or

based on choice)

Page 101: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

[False,

"Just like every morning, Miss Muffet was

famished. Luckily, just like every morning, she

had brought her curds and whey to enjoy for

breakfast.",

"SpoonMouth"]

Page 102: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

[False,

"Just like every morning, Miss Muffet was

famished. Luckily, just like every morning, she

had brought her curds and whey to enjoy for

breakfast.",

"SpoonMouth"]

There are no choices in this scene.

Page 103: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

[False,

"Just like every morning, Miss Muffet was

famished. Luckily, just like every morning, she

had brought her curds and whey to enjoy for

breakfast.",

"SpoonMouth"] All scenes have their scene text to print

Page 104: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

[False,

"Just like every morning, Miss Muffet was

famished. Luckily, just like every morning, she

had brought her curds and whey to enjoy for

breakfast.",

"SpoonMouth"] This is the next scene to visit

Page 105: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

[True,

" As the spider landed on Miss Muffet’s spoon,

the girl sensed something was wrong.",

"Miss Muffet should run away as soon as she sees

the spider.",

"RunAway",

"Miss Muffet should stay to greet the spider.",

"Stay"]

Page 106: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

[True,

" As the spider landed on Miss Muffet’s spoon,

the girl sensed something was wrong.",

"Miss Muffet should run away as soon as she sees

the spider.",

"RunAway",

"Miss Muffet should stay to greet the spider.",

"Stay"]

This scene does have a choice

Page 107: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

[True,

" As the spider landed on Miss Muffet’s spoon,

the girl sensed something was wrong.",

"Miss Muffet should run away as soon as she sees

the spider.",

"RunAway",

"Miss Muffet should stay to greet the spider.",

"Stay"]

Scene text

Page 108: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

[True,

" As the spider landed on Miss Muffet’s spoon,

the girl sensed something was wrong.",

"Miss Muffet should run away as soon as she sees

the spider.",

"RunAway",

"Miss Muffet should stay to greet the spider.",

"Stay"]

Option 1

Page 109: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

[True,

" As the spider landed on Miss Muffet’s spoon,

the girl sensed something was wrong.",

"Miss Muffet should run away as soon as she sees

the spider.",

"RunAway",

"Miss Muffet should stay to greet the spider.",

"Stay"]

Next scene if option 1 is chosen

Page 110: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

[True,

" As the spider landed on Miss Muffet’s spoon,

the girl sensed something was wrong.",

"Miss Muffet should run away as soon as she sees

the spider.",

"RunAway",

"Miss Muffet should stay to greet the spider.",

"Stay"]

Option 2

Page 111: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

[True,

" As the spider landed on Miss Muffet’s spoon,

the girl sensed something was wrong.",

"Miss Muffet should run away as soon as she sees

the spider.",

"RunAway",

"Miss Muffet should stay to greet the spider.",

"Stay"] Next scene if

option 2 is chosen

Page 112: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Now we have scene data – how can we associate the

scene name with the data?

Page 113: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Dictionaries

The dictionary data structure associates one piece of data with

another.

Page 114: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Dictionaries

myDictionary = {"one" : 1, "two" : 2, "three" : 3}

Creating a new dictionary:

Page 115: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Dictionaries

myDictionary = {"one" : 1, "two" : 2, "three" : 3}

Creating a new dictionary:

Keys

(similar to words in a dictionary to look up)

Page 116: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Dictionaries

myDictionary = {"one" : 1, "two" : 2, "three" : 3}

Creating a new dictionary:

Values

(similar to the definition found when looking up the word)

Page 117: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Dictionaries

myDictionary = {"one" : 1, "two" : 2, "three" : 3}

Creating a new dictionary:

Page 118: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Dictionaries

print(myDictionary["two"])

Accessing values in a dictionary:

Page 119: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Dictionaries

print(myDictionary["two"])

Accessing values in a dictionary:

Prints 2

Page 120: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Dictionaries

myDictionary["two"] = 22

Replacing values in a dictionary:

Page 121: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Dictionaries

myDictionary["ten"] = 10

Adding new values to a dictionary:

Page 122: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

Dictionaries

for key in myDictionary.keys():

print(myDictionary[key])

Printing values in a dictionary:

for value in myDictionary.values():

print(value)

print(list(myDictionary.keys()))

print(list(myDictionary.values()))

Page 123: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

What is the value of d after this code runs? d = {3:4, 4:5, 5:6}

d[3] = 8

d[6] = 7

Text 37607 729146: {3:4, 4:5, 5:8} 729147: {3:8, 4:5, 5:6} 729151: {3:4, 4:5, 5:8, 6:7} 729152: {3:8, 4:5, 5:6, 6:7} 729153: Nothing / error

Page 124: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

References

Page 125: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

A Look at Our Data's Structure

Page 126: Variables, References and Data Structures

COMP 1001: Introduction to Computers for Arts and Social Sciences

A Look at Our Data's Structure