Session 1 - User Input

advertisement
Session 1
User Input in Alice

Multi-character input - This is used when the user is
prompted to enter a number or word.

Issues with multi-character input - Possible problem with
type differences.

Single-character input – This is used to trigger specific
events (an example may be moving an object when an
arrow key is pressed)
Multi-character Input
Quick Explanation of Types

In many high level languages such as C++ and Java, there
are different types of data.

For example, a word would be a different type of data
when compared to a number. Words and sentences are
categorized as Strings. Numbers are categorized as
integers.

Oddly enough, this makes things more organized when
programming. In languages that don’t have this capability,
things can get quite confusing and the program can have
unexpected results.
Input using the “Ask User …”
Blocks

Now we will explain the “ask user” section of the world’s
functions but first we need to start a new project. Create a
new world by clicking “file” in the top left of your window
and then clicking “new world.” If you haven’t saved your
previous project yet, the Alice program should now ask you
to save. Save everything to your flash drive.

Please proceed in setting up your program by creating an
object and positioning it to your liking. Exit object
management mode, select the world by either clicking it in
the objects tree or on the sky in the world view.

Click the functions tab and scroll down until you find the
“ask user” section.
Input using the “Ask User …”
Blocks

There are three blocks under ask user. These blocks allow
the programmer to ask the user specific questions in which
the answer could be a number(
), a string
(
), and finally a window with yes or no clickable
buttons(
).

First we want the object we have created to greet the user,
then ask the user’s name and say something in response
using that name.

Look under your object’s methods tab and find an
block and drag it to the methods editor area.
Input using the “Ask User …”
Blocks

This menu should appear.
You can either choose one
of the predefined sayings or select “other” to create your
own. Now test our project to make sure things are going
right so far.

Now we have the object saying something, it is time to ask
a question. Place another say block below the one you
previously placed and choose anything for it to say.

Go to world functions and find
. This will allow us
to ask the user a question and get a string (a word or
sentence) as a response.
Asking a Question

Drag this block into the box directly after the word say.
Both boxes should turn green confirming that the item
being placed into the say block is of the correct type.

When you drop the block, this menu should appear:

This is similar to the previous slide. Simply select “other”
to type another question that isn’t already shown. Enter
the question “what is your name?”
Asking a Question

Let us test the project now. Notice that after the question
is asked, only the name is said by the object. This is
because we need something such as “nice to meet you
___________________.”

To account for this, we must drag the ask block we
previously placed inside the say block to the top-right
corner of the window and place it on the clipboard.
Now right click the original block and select delete.

The clipboard is a tool that allows the programmer to store
snippets of code that they may be able to use later rather
than re-constructing them. If you now try to drag from the
clipboard, whatever you dragged to it last appears and you
can now move it to its desired location.
Asking a Question

To create this sentence we will need a block that allows us
to join two strings together. The first string will be
something like “Nice to meet you ” and the second string
will be the question asked. Under world functions, locate
the string section.

Drag an
block to the box after the word “say.”
(This should be the same place we put the ask block
previously) A familiar menu should pop up and you should
select “default string.”
Asking a Question

We should have something that looks similar to this.

These arrows allow the programmer to
change the value of the strings being joined.

It is also possible to drag a string
variable (this will be covered later) or any function that
returns a string to these boxes.

Click the first arrow in the “joined with” block, select other
and type something like “nice to meet you ”.

Now drag the code from the clipboard and place it into the
second box of the “joined with” block.
Asking a Question

Let’s look at what we have so far:

It appears as though the object will say “Nice to meet you”
afterwards asking the question “What is your name?” What
really happens? The object can’t say something it hasn’t
received an answer for. In response, the program will first
ask the user the question. After an answer from the user,
the object then joins the first part of the “joined with”
block with the answer supplied by the user. Let’s go ahead
and test this now.
Using number input

Number input, another type of multi-character input, plays
a vital role in a programmers’ life. It allows the
programmer to ask the user to input a number which can
be used later in the program (once we discuss variables).

Adding to our previous program, we can use the “ask user
for number” block placed inside of a “move” block. The
programmer will ask the user how far to move and the user
will enter a number. The object will then move that
distance.

Let’s start constructing this now. Make sure your object is
selected and drag a “move” block underneath the blocks
we previously placed for string input. It should ask you
which direction to move, just choose one you think will be
visible in the world window. When it asks you for distance,
pick anything because we will soon be replacing that
number.
Using number input

Now drag a “move” block to the box containing the number
of meters to move.

You can select the predefined question or select “other” to
create your own.

Now let’s test our project to see what happens. The
program should successfully move the object the distance
specified by the user. It may have moved very fast if the
user inputs a large number or not at all if the user puts in a
decimal number.

Note: If you want to enter a decimal number with an
absolute value less than 1, there needs to be a leading
zero before the decimal.
Type difference issues

Run your program again and instead of entering a number
when asked for a number, enter a string (word/sentence).

Notice the object doesn’t move. This is what we discussed
earlier with different types of data. The program is
expecting a number so the object knows how far to move.
If a string is entered, the program doesn’t know what to
do so it will do nothing. Interestingly enough, if you type a
number when asked for a string, it will print the actual
number as we would expect.

There could be other type conflictions but this is one of the
most prominent for Alice.
Single-character Input
Single-character Input

Now we will discuss single-character input. As briefly
explained earlier, single-character input is used to trigger
certain events. An example we will soon construct adds to
our current program and allows the user to move their
object around the world using the arrow keys.

First we need to create a new event. To do this, look in the
events area and click the button “create new event.” A
menu will drop down which will have numerous options.
Choose the option “when a key is typed.” The events area
should look like this:
Single-character Input

:This box represents the key which will
be used to trigger a specific event.

:This box represents what is being
done when that key is pressed.

Since we want to move the object when a key is pressed,
drag a “move” block from your object’s methods and place
it into the second box of the “when key is typed” block.
Choose to move right and choose 0.1 for the
distance.(Note: It may be necessary to manipulate the
object so it moves in the correct direction from your point
of view.)
Single-character Input

Now click the drop-down arrow in the first box which should
contain “any key.” In the drop down menu choose “right.”

Now test your project and see what happens.

The object should move right only after the key is released. To
account for this, right click the “when key is typed” block and
change it to “while key is pressed.” Now drag a move block
from your object’s methods and place it into the “During” box
of the “While key is pressed” block. Again we will need to
choose the direction (right) and the distance (0.01).

If you left click the arrow to the right of “more” inside of the
move block, you have more tools at your fingertips. For
example, click the arrow, mouse-over style, and choose
abruptly. Now test your program and see the difference.

Go ahead and create 3 more events which allow the object to
move around the world window.
Project 1

Create a small animation using single and multi-character
inputs. Use single-character input to key events or move
an object about the screen. Use multi-character input to
ask a question, the object must respond to the question
using the answer to the question (similar to the example
done earlier).
Day 1 Questions
1.
In a method/function, in what order are blocks executed?
2.
What are the three main elements of an object?
3.
Can objects be created, destroyed, or deleted once the
program has started?
4.
Can multiple blocks run at the same time? If so, how
could this be accomplished?
5.
What are two different types of input in Alice?
6.
Single-character input can be used to trigger a
method/function.(__true/__false)
7.
Asking for a name is an example of what kind of input.
Answer Key
1.
In a method/function, in what order are blocks executed?
Top to bottom,sequentially,in order
2.
What are the three main elements of an object? Properties
(variables), methods, and functions
3.
Can objects be created, destroyed, or deleted once the
program has started? No
4.
Can multiple blocks run at the same time? If so, how could
this be accomplished? Yes, do together container
5.
What are two different types of input in Alice? Multicharacter and single-character input
6.
Single-character input can be used to trigger a
method/function.( X true/__false)
7.
Asking for a name is an example of what kind of input. Multicharacter input
Download