Input Types

  1. In Lab 2, you created a block animation of max of A and B, showing results max of (x) and (y) that reports which of its two inputs is the greater number (the maximum). Load that block now or quickly recreate it.

The max block has an unexpected behavior. It is intended to work only for numbers, yet you can type text into it! Max of cat and dog outputs dog

You have already used is input a number to check for situations like this. You can also define blocks to show what the expected input type is (number, text, etc.).

    Editing input 'x'
  1. Edit the max block. In the editor, click on the input name x.
  2. You can also specify a default value for the variable if you like. This is how the move block was defined always to start as move 10 steps with the default value 10, and not just move steps with no default value specified.
    Then, click the right-arrow in the pop-up box. The dialog lets you choose the input type you want. Click the Number slot. The shape of the slot tells people what type of input the block expects. Input Type
  3. Change both x and y to take only numbers and to have the default values of 5 and 10. You will see this in the Block Editor.
    The variable names are still x and y, even though the block's title line looks different. The # indicates that the input should be a number; the "= 5" shows the default value.

    'max' block with defaults

    When you click OK, your block should look like this max of 5 and 10. You can no longer type text as input.

  4. When an input type is restricted, a block can still receive improper values. max of true and 10 snap doesn’t constantly check to see what types are passed as values. Specifying the input type is an organizational tool to remind you what the domain (input values) of your blocks should be.

    If you have a job writing computer programs that do something people rely on, a lot of the effort goes into making sure all your inputs are of the correct type. This can get complicated; maybe the input has to be a positive number; maybe it has to be a positive number that's a multiple of 1/100 (i.e., an amount expressed in dollars, but a whole number of cents). It's not unusual for the error-checking code to be longer than the part of the program that actually does the work. But in this course it's generally okay to ignore possible input errors, as long as your programs work correctly for correct inputs. We want to keep you focused on the interesting parts of the projects.

  5. Edit a greater than or equal to b and n factorial and between and integer? to restrict their input values to numbers.