CS02 : Dealing with Input

A practical lesson introducing the Python programming environment focusing on the distinction between the programmer and the user, and writing suitable prompts to get user input. There is also an extension activity at the end which deals with a variety of input devices and sensors.

https://docs.google.com/presentation/d/10x_cRkMNorHR9LVV_043UTEXPIbzz9xboxnrAAKoyAc/preview?slide=id.g3cb6bc7fc1_0_0Learning Outcomes
Dealing with Input

We are learning ...
  • How to write simple code which accepts and responds to user input
  • About the use of pseudo code for assignment using 
  • About variables and constants
  • About the purpose and function of input devices
So that we can ...
  • Write suitable statements to ask computer users for information
  • Understand what assignment involves and how to represent it using pseudocode
  • Write programming statements which accept user input from the keyboard.
  • Describe the advantages of using named constants in scripts
  • Identify, describe applications for and the function of various input devices and sensors.

# Get Ready.png

https://docs.google.com/presentation/d/10x_cRkMNorHR9LVV_043UTEXPIbzz9xboxnrAAKoyAc/preview?slide=id.g3df8b56503_0_5Activity 1
Who are you?

You are here to learn about computer science. That means that you will need to take on two roles - that of the user of the computer and that of the programmer.

PROGRAMMER : Generally an expert in programming, is in charge of solving the problem and making sure that she gets all the information she requires from the users.  Has a fixed idea of what the program should do.

USERS : Generally not experts in programming, don't know (or care) how the program works as long as they get the right answer at the end.  Will often not have time to read user manuals and so will need to be given lots of prompts as to the information that the programmer requires to solve their problem.  Often impatient.

Task 1.1 Thinking about roles
Where we learn about the difference between programmers and users

As a class

Consider the different needs of a computer user and a computer programmer. What does the programmer want? What do the users want?  Pair up with a partner and discuss.  Be prepared to share your ideas with the rest of the class.

https://docs.google.com/presentation/d/10x_cRkMNorHR9LVV_043UTEXPIbzz9xboxnrAAKoyAc/preview?slide=id.g41ed5504bf_0_1Activity 2
In programming, memory is 'named'

It is the programmers job to gather the data that he / she requires from the users of the computer to enable the problem to be solved.  He / she needs to think carefully about what that data is and how it should look.  

The data that the computer program may need to gather describes characteristics of an object.  

The data (or rather the memory the data is stored in) has to be given a name to help us and the computer to identify it later on.  These are called identifiers (or names in some programming languages).  There are two types of identifiers which are variables (identifiers who's values can change) and constants (identifiers who's values can't change).

Task 2.1 Worksheet (number one)
Where we learn how to describe objects by identifying their important characteristics

On the worksheet

Download and complete the worksheet, Describing ThingsThis exercise is designed to help you identify named characteristics of objects. Make sure you check your answers before handing the worksheet in for checking!

Assignment is the name used to describe the method used to store data in a computer system by putting it in the computers memory and attaching an identifier to it so it can be found later on.  You can think of it as attaching a label to your keys to help you find them in the key box ...

Programmers use a simple language called pseudocode to write assignment statements to show how data is stored and, most importantly, how it is identified by the computer program ...

shoesize ← 8.5

width ← 13
name ← 'Mark'
finished ← True

The first statement can be read "store the value 8.5 in the computers memory and label it shoesize so we can find it again later if we need it" - that's a bit long winded which is why we would tend to write shoesize ← 8.5Notice the leftwards facing arrow - this is called an 'assignment operator'.

The names are sometimes known as variables or identifiers because the values they identify can vary ...

names / variables / identifiers

Task 2.2 Worksheet (number two)
Where we learn about assignment and how computers store data in memory

On the worksheet / as a class

Download, print and complete the worksheet Assignment as a class. You will need to concentrate!

When you ask the user for data, the programmer needs to use command prompts rather than questions and usually needs to give the user some idea what the data should look like so that they can attempt to type it in in the correct way.  For example ...


If I want the user to select a certain category of film for instance, I might write a command prompt like this ...

"Select category (A, B or C) : "

Command prompts are a like closed questions in a questionnaire.

Task 2.3 Worksheet (number three)
Where we learn how to write suitable input prompts

On the worksheet

Download and complete the worksheet Prompts.

https://docs.google.com/presentation/d/10x_cRkMNorHR9LVV_043UTEXPIbzz9xboxnrAAKoyAc/preview?slide=id.g41ed5504bf_0_6Activity 3
The computer wants what?

Task 3.1 Asking for stuff
Where we learn practical methods for getting data from the user

The Python programming environment (which hopefully you have already met) allows us, the programmers, to ask them, the users, for information. Sometimes, the confusing thing is that you can be the programmer and the user - you are both at the same time!

Carry out the tasks using the Python programming environment, IDLE. Record what you are doing in a word processed document using a combination of screenshots and written explanation.

Get ready to code

Open the Python programming environment, IDLE, use the online  TRYTHON  shell or create a new Repl.

This is called the shell and the three chevrons (>>>) are called the prompt. To the right of the prompt should be a flashing cursor which is where we type instructions. The shell is used mainly for development and trying things out.

At the prompt

When we use assignment statements in Python, we don't use the leftwards facing arrow (←) that we use in pseudocode (can you find it on the keyboard?). Instead, we use an equals sign,  = .

Try typing the following commands exactly, one at a time at the prompt, pressing
 Enter   after each one (don't type the >>> - that's already there!) ...

>>> forename = 'John'
>>> print(forename)
>>> forename = 'Fred'
>>> print(forename)

In your word processed document

Write about what happened. Make sure you use the word assignment and the word reassignment in your explanation.

At the prompt

Rather than assigning the value of forename explicitly, we now know how to ask the user for it using a command prompt. Python uses the instruction input(prompt) to allow us to do this. Try typing the following input commands exactly as written, one at a time, at the prompt, pressing  Enter  after each one. In the second line, the blue text will appear - just type John and press the  Enter  key.

>>> forename = input('Enter your forename (no spaces) : ')
Enter your forename (no spaces) : John
>>> print(forename)

Now try this again but respond using a different name than you typed before ('Fred' for example) and see what happens this time.

In your word processed document

Write about what happened, using this Python code as an example. Try to use the word 'reassignment' in your explanation. How is this method better than the first one you tried? Print out your word processed document for your notebook / folder.

Task 3.2 Silence and whiteboards
Where we learn about the rules for naming 'identifiers'


On paper / in your notebooks

Read the passage below and make some handwritten notes on paper in silence ...

Rules for 'legal' identifiers in Python
When you are writing variable names in Python, there are a couple of rules you need to stick to.  
    • Identifiers can only contain letters and numbers.  
      • Identifiers cannot start with a number or contain any spaces.
        • Python is case sensitive which means that a capital letter is different from a lower case letter - i.e. a 'T' is different from a 't', so the identifier 'toenails' is different from the identifier 'Toenails'.
        Programmers often use camelCaps format to write identifiers which are made up from more than one word.

        For example : 
        • Shoe size is written as shoeSize
        • Menu choice would be written as menuChoice
        • Length of skirt would be written as lengthOfSkirt

        As a class

        Now carry out the activity as a class with your whiteboards.

        Task 3.3 Practical examples of input statement
        Where we learn practical methods for writing input statements

        For each of the following situations, construct a suitable input statement to gather the required data, type this in at the shell, provide a suitable value to test the statement works and then echo the value (using print()). Use the examples you tried earlier to help you. 

        For example, to ask the user to enter their hair colour ...

        >>> hairColour = input('Enter your hair colour (brown, black, blonde) : ')
        Enter your hair colour (brown, black, blonde) : black
        >>> print(hairColour)

        For this exercise, create a word processed document with a suitable header and footer. Record what you have done using a combination of screenshots and written explanation.

        Get ready to code!

        Open up the Python programming environment, IDLE or you can use the Python programming environment, IDLE or use the online  TRYTHON  shell - whichever you prefer.

        At the prompt

        Construct suitable prompts to store and print out the user's choices in each case.  Document what you have done using screenshots and written explanation in a word processed document. I have done the first one for you ...
        • Shoe size

          >>> shoeSize = input('Enter your shoe size (6,7,8) : ')
          Enter your shoe size (6,7,8) : 6
          >>> print(shoeSize)

        • Menu choice
        • Length of skirt
        • Number of stripes
        • Engine size
        • Trussing width
        • Outer edge girth
        • Leg count


        Print out the word processed document for your notebook / folder.

        https://docs.google.com/presentation/d/10x_cRkMNorHR9LVV_043UTEXPIbzz9xboxnrAAKoyAc/preview?slide=id.g41ed5504bf_0_11Activity 4
        What about constants?

        The first thing to realise is that constants are not variables!  A constant is a value stored in memory that is identified by an identifier (name) but which cannot / should not be changed. But why would we need them?

        Task 4.1 What's the point of CONSTANTS?
        We we learn about the advantages of using names constants

        In a Python script

        Your teacher will remind you how to create a Python script. Type the following code in the script (you can copy and paste it if you want to!)

        LANGUAGE = "Python"

        print("My favourite programming language is " + LANGUAGE + " because ")
        print(LANGUAGE + " allows me to write computer programs for my friends!")
        print("Also, " + LANGUAGE + " code is really easy to write because the")
        print("instructions in " + LANGUAGE + " look like English words. I do")
        print("have to be careful when I'm writing " + LANGUAGE + " statements")
        print("that I get the syntax correct or the " + LANGUAGE + " program won't work.")

        Can you see that the name LANGUAGE is used to identify the word "Python" in memory? The identifier is written in CAPITAL LETTERS to tell us that it is a constant. How many times is LANGUAGE used in the script?

        Save the script in a suitable place in your documents and run it by pressing the  F5  key on the keyboard. What do you see? What has printed instead of the constant LANGUAGE in the print() statements?

        Edit your script

        Now try changing the first line in the script to ...

        LANGUAGE = "Javascript"

        ... save and re run the script by pressing the  F5  key on the keyboard. What has happened? Can you see the advantages of using a constant for the language name?

        In a new script

        Type the following code in a new Python script (you can copy and paste!)

        PI = 3.14159265358979323846

        radius = input('Enter the radius for your circle (mm) : ')
        radius = int(radius)
        diameter = 2 * radius
        circumference = PI * diameter
        area = PI * radius ** 2

        print('Your circle has a diameter of : {0:.2f} mm'.format(diameter))
        print('Your circle has a circumference of : {0:.2f} mm'.format(circumference))
        print('Your circle has an area of : {0:.2f} mm squared'.format(area))

        Can you see where the constant PI is used in this script? Why do you think this is better than using it's value in the calculations for circumference and area?

        On the worksheet

        Download, print and complete the worksheet CONSTANTS (it's in capitals on purpose!)

        https://docs.google.com/presentation/d/10x_cRkMNorHR9LVV_043UTEXPIbzz9xboxnrAAKoyAc/preview?slide=id.g46b729ff65_0_46Activity 5
        Input devices and sensors

        The IPSO model of computer systems with the inputs highlighted in green

        Getting data into a computer system is kinda fundamental. Without data to process, the computer can't do anything useful. One place data comes from are manual input devices. Here is a list of some common ones ...
        • 2D and 3D scanners,
        • Barcode readers,
        • Quick response (QR) code readers,
        • Digital cameras,
        • Keyboards,
        • Mice,
        • Touch screens,
        • Interactive whiteboards,
        • Microphones
        ... and here is a list of automatic input devices or sensors ...
        • Light
        • Temperature
        • Magnetic field
        • Gas sensor
        • Pressure sensor
        • Moisture sensor
        • Humidity
        • pH (acidity / alkalinity)
        • Motion
        A sensor is a device that measures analogue physical or environmental characteristics and may convert them into a digital signal as well - sensors detect changes in the environment.  The input devices we have seen so far have involved a form of human interaction.  Sensors do not need this human interaction, instead they respond to the environment automatically.

        The input device or the sensor is responsible for converting the analogue world into a digital world for processing by the computer itself. This analogue to digital conversion either happens in the computing device or in a separate device designed to carry out that specific process.

        Task 5.1 Input devices and sensors
        Where we learn about the identity, applications and function of various input devices

        Draw some mind maps

        Convert the lists above into two mind maps. Use lots of colour and pictures to make your mind map more interesting to look at - this will also mean that it will be easier to remember. Try to think about the situations in which each device or sensor may be used and add this to your map as well.

        Peer assess your map

        Give your maps to your shoulder partner and ask him / her to tell you want went well and what could be improved. Make sure you make the improvements suggested and hand in both maps to your teacher.

        Task 5.2 Waymo - Google's self driving car project
        Where we learn about the application of sensors to a contemporary situation

        Fully self-driving Jaguar I-PACE electric SUV 3
        from Waymo

        Visit the Waymo website and do some more research

        Visit the Waymo website which is the home of Google's self driving car project. Investigate and explain what input devices (including sensors) you might need in a driver-less car of the future. Try and think about the following:
        • Touch screen 
        • Microphone 
        • Temperature sensor 
        • Humidity sensor 
        • 2D scanner 
        • Lidar

        Present your research as a poster

        Use all the research you have collected to make a poster about the application of sensors to the concept of the self driving car. Make sure you mention how each of the sensors listed (and any others if you can find them) are used in this, or other, autonomous vehicles. Hand your poster into your teacher when you have finished.

        Assessment Task (Homework)

        Create a questionnaire to gather information from your friends about a favourite topics. However, instead of asking questions, use command prompts instead and either give the respondents a choice of what answers they are allowed to give or tell them what format they should answer in. For instance ...

        Good prompt : 'Choose your favourite film actor (Bronson, Smith, Brown) : '
        Poor prompt : 'Who is your favourite film actor?'

        You questionnaire should have 5 questions and could be hand written or done on a computer. You can choose the topic yourself but you might want to use these suggestions.
        • TV Shows
        • Films
        • Music
        • Computer games
        • Places to visit
        Grading rubric

        MASTER :  I have created a questionnaire that contains 5 closed, command prompt style questions.  My questionnaire is clearly laid out and would be easy to complete.
        APPRENTICE I have created a questionnaire that contains less than 5 closed command prompt questions or the 5 questions I have asked are not commands and do not give an indication of how they should be answered.
        NOVICE : I have created a questionnaire but less than half of the questions are command prompt questions and I have not given clear enough indications of how the respondent should answer them.

        # Flash cards.png
        Click to load key word list to help you make your own flash cards 

        Hungry for more?

        Try having a conversation with someone using only command prompts.  It's (a) difficult and (b) quite rude!

        Robots also use sensors to detect their environment. There are a lot of videos on YouTube from Boston Dynamics which you should watch and be amazed! Talk to your computing teacher about what you have seen.

        FAQ Frequently Asked Questions

        Q : Are there alternatives to camelCaps?
        A : It's absolutely fine to use other systems; quite often, programmers separate the words in the variable with underscores and avoid capital letters altogether. As long as there are no 'illegal' characters, it's fine.

        Q : What is the maximum number of variables you can have in a script?
        A : The only limit to the number of variables you can have in a script is the available memory required to store them.

        Q : Why do we use pseudocode?
        A : Good question! We use pseudocode because it is programming language 'non-specific' and should allow anyone to convert the algorithm into any programming languages. It literally means 'false code'.