CodeSports

3.0-Basic Input Overview

Basic Input:

Students learn how to use the input() command to assign strings to variables.

Input

This topic adds the input command to the prior learning about print and variable assignment.

Learning Objectives/Goals

Students will learn to interpret, trace, identify and create python code that uses:

Concept Explanation Python Code Examples
Strings The text data type. All string data is treated as text, even if it is numerical characters. “string goes here” “This is a string”
Variable assignment Putting data into a variable. = num1 = 10 name = “Andy”
Concatenation Joining string literals (text in speech marks that prints exactly as it appears in the code) to the contents of variables in output, or joining the contents of variables together. There are several ways of doing this, but I’ve used the plus symbol in my exercises. + print(“Hello ” + name) fullName = firstName + lastName
Input Collecting data for processing. In these examples we collect input that the user types on the keyboard but computers collect input in lots of ways – as sound, mouse movements, with sensors etc. ** Input must ALWAYS be assigned to a variable** or it will not be stored. All input is treated as strings. Any input that needs to be treated as a number must be cast into the new format. variableName = input() print(“What’s your name?”) name = input() print(“Guess the secret number” guess = int(input())

Slide Deck

[The slide deck for this topic can be found here] (https://docs.google.com/presentation/d/1iQtjLPF7ZCsytrZSkp3I_v9uE4SvYNA8x5TQLKQETJU/edit?usp=sharing)

Activities

Each skill has 3-4 individual projects associated with it that take students through the PRIMM pedagogical framework. They are numbered to try and make the order obvious. You can assign these one at a time or drip feed as students get to them.

TEACHER NOTES

Prior Learning

  • Algorithms – Sequences of instructions for a task. You can learn more about algorithms here:

https://www.w3schools.blog/data-structure-algorithm#:~:text=A%20procedure%20having%20well-defined%20steps%20for%20solving%20a,in%20order%20to%20accomplish%20a%20certain%20predefined%20task.

  • Output/Print – The information presented to the user by the computer. In our programs output will be in the form of text on screen. You can find more about output/print here:

https://www.w3schools.com/python/ref_func_print.asp

  • Strings – Data in text format. You can find out more about strings here:

https://www.w3schools.com/python/python_strings.asp

  • Syntax – the format that code is written in. If code is not written in the correct format it creates a **syntax error **and the program will not run. You can find more about syntax here:

https://www.w3schools.com/python/python_syntax.asp

  • Variable – a placeholder for information that can change as the program runs. You can find out more about variables here:

https://www.w3schools.com/python/python_variables.asp

  • Variable assignment – Using the ‘=’ symbol to store data in a variable. You can find out more about variable assigment here:

https://www.w3schools.com/python/gloss_python_assignment_operators.asp

  • Concatenation – joining the contents of variables to other variables and/or strings in output.

New Learning

  • Input – the data collected by the computer. In our programs input will be in the form of the user typing in. Input must be assigned to a variable or it will be discarded by the program.

There are lots of similar tasks available online that you can use to supplement the ones in these resources. I can particularly recommend:

https://www.w3schools.com/python/python_exercises.asp

https://www.practicepython.org/

The input/process/output diagram on the lesson slides will be referred back to in future sessions.

It’s important to ensure that students understand the difference between algorithm and program.

Once you have explained this we move on to coding output using the print statement. This is one of the simplest commands in Python. It offers instant feedback (users can see if it works or not straight away). When students have had a chance to predict & test, start to explore how to break the statement. This will give you a chance to highlight the importance of precision and correct syntax in programming. I find it really helps if students appreciate this very early on.

At first, you start with printing set text (called strings). In later tasks you use variables to store text and refer back to it. Variables are used in a very similar way to letters in algebra – they represent the text. Students can call variables anything they like, but there are some good practice principles:

Help! My Code Does Not Work!

Make sure to look out for the following things:

  • Students put the data into the brackets after the input command eg:
print("What's your name?")
name = input("Andy")
  • Missing brackets after the ‘input’
  • Not pressing enter after typing in the input.
  • Not assigning the input to a variable

Hints