strings - duke computer science · 2008-07-25 · the cat is going to ask the user the last name...

16
Strings Strings Strings are sequences of characters and / Strings are sequences of characters and / or digits AliceAlice “412-555-9999” I would like 3 peachesI would like 3 peaches When a model says or thinks, it is using a ti string

Upload: others

Post on 11-Mar-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Strings - Duke Computer Science · 2008-07-25 · The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

StringsStrings

Strings are sequences of characters and /Strings are sequences of characters and / or digits

“Alice”Alice“412-555-9999”“I would like 3 peaches”I would like 3 peaches

When a model says or thinks, it is using a t istring

Page 2: Strings - Duke Computer Science · 2008-07-25 · The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

duckPrince saysduckPrince says…

String

Page 3: Strings - Duke Computer Science · 2008-07-25 · The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

ScenarioScenarioHave the Cheshire Cat greet the user by nameOpen world CheshireQuiz.a2w, methodgreeting

Page 4: Strings - Duke Computer Science · 2008-07-25 · The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

Create String variable for the user name

Click to create new variable

Enter variable name, select type

Page 5: Strings - Duke Computer Science · 2008-07-25 · The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

Ask user for a stringAsk user for a string

Drag variable tile (World’s details, Properties tab) into editor, select set value to option

Drag “ask user for string” tile (World’s details, F ti t b) i t dit t tiFunctions tab) into editor, enter question

Page 6: Strings - Duke Computer Science · 2008-07-25 · The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

Display user name in stringDisplay user name in string

We start by having the cat say “Hello, ” (note the space)

Page 7: Strings - Duke Computer Science · 2008-07-25 · The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

Build the new stringBuild the new string

From World’s details, functions, drop “a joined with b” tab after say

From World’s details, properties, drop userName variable

Page 8: Strings - Duke Computer Science · 2008-07-25 · The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

Run the WorldRun the WorldHow was the exclamation point added to what the cat said?

Page 9: Strings - Duke Computer Science · 2008-07-25 · The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

Getting User InputGetting User Input

Events allow users to interact with the project byEvents allow users to interact with the project byManipulating the mouseMaking choices using the keyboard g g y

We may also want to get information from the user that is not event based.

Personalize a story by asking the user for her nameAsk the user for a numberAsk the user to make a decision

Page 10: Strings - Duke Computer Science · 2008-07-25 · The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

Conditional Execution (review)Conditional Execution (review)Conditional execution is where some condition is checked and a decision is made about whether a block of the program will be

t dexecuted.Conditional execution is extremely useful in

gamessimulations

l ti t l b t treal-time controls, e.g. robot systems

Page 11: Strings - Duke Computer Science · 2008-07-25 · The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

Ask User for yes or noAsk User for yes or no

The cat is going to ask the user a question thatThe cat is going to ask the user a question that can be answered with a yes or no

Yes => true, no => falseIf the cat likes the answer, the cat will celebrateIf the cat does not like the answer, the cat will didisappear

World: cheshireQuiz.a2wM th d N Q tiMethod: yesNoQuestion

Page 12: Strings - Duke Computer Science · 2008-07-25 · The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

Ask user for yes or noAsk user for yes or no

Declare variable to get user responseDeclare variable to get user response

U f A k f f tiUse of Ask user for yes or no function

What user sees

Page 13: Strings - Duke Computer Science · 2008-07-25 · The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

Ask user for a numberAsk user for a number

The cat is going to ask the user anThe cat is going to ask the user an arithmetic question

If the user types in the correct answer the catIf the user types in the correct answer, the cat will celebrateIf the user types in the wrong answer the catIf the user types in the wrong answer, the cat will disappear

World: cheshireQuiz a2wWorld: cheshireQuiz.a2wMethod: numberQuestion

Page 14: Strings - Duke Computer Science · 2008-07-25 · The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

Ask user for a numberAsk user for a number

Declare variable to get user responseDeclare variable to get user response

U f A k f b f tiUse of Ask user for a number function

What user sees

Page 15: Strings - Duke Computer Science · 2008-07-25 · The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

Ask user for a StringAsk user for a String

The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

Remember, strings are case sensitive; implications for checking the user responseIf the user types in the correct name, the cat will celebratecelebrateIf the user types in the wrong name, the cat will disappearpp

World: cheshireQuiz.a2wMethod: stringQuestion

Page 16: Strings - Duke Computer Science · 2008-07-25 · The cat is going to ask the user the last name ofThe cat is going to ask the user the last name of the author of “Alice in Wonderland”

Ask user for a StringAsk user for a String

Declare variable to get user responseDeclare variable to get user response

U f A k f St i f tiUse of Ask user for a String function

What user sees