0% found this document useful (0 votes)
4 views35 pages

Input

This lesson focuses on obtaining input from the keyboard in programming, differentiating between various data types such as integer, real, Boolean, character, and string. It covers the importance of casting variables and defining runtime errors and validation checks. Additionally, it provides examples of how incorrect data types can lead to errors and emphasizes the need for validation checks to prevent such issues.

Uploaded by

19koudasm.ps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views35 pages

Input

This lesson focuses on obtaining input from the keyboard in programming, differentiating between various data types such as integer, real, Boolean, character, and string. It covers the importance of casting variables and defining runtime errors and validation checks. Additionally, it provides examples of how incorrect data types can lead to errors and emphasizes the need for validation checks to prevent such issues.

Uploaded by

19koudasm.ps
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Computing

Lesson 4: Input

Programming Part 1: Sequence

Rebecca Franks

Materials from the Teach Computing Curriculum created by the National Centre for Computing Education
In this lesson you will:

Obtain input from the keyboard in a program

Differentiate between the data types: integer,


real, Boolean, character, string

Cast variables by calling a function that will


return a new value of the desired data type

Define runtime errors in programs

Define validation checks

2
Make predictions
Questions .
What will be the output of print, when
age = 22 this program is executed?
print(f"My age is {age}") A. My age is age
B. My age is 22
C. It is not possible to know the
output without executing the
program
D. There is an error in the program

3
Make predictions

Questions .
What will be the output of print, when
print(f"My age is {age}")
this program is executed?
age = 22
A. My age is age
B. My age is 22
C. It is not possible to know the
output without executing the
program
D. There is an error in the program

4
Make predictions

Questions .
What will be the output of print, when
age = 22
this program is executed?
age = 23
print(f"My age is {age}") A. My age is 45
B. My age is 22,23
C. My age is 23
D. There is an error in the program

5
Make a prediction

1 print("What is your name?") Question .


2 name = input()
3 print(f"Hello {name}") Take a look at these three lines of code.

● What do you think each line of


code will do when executed?
● What would be the output of line
3?

6
Walking through the code

1 print("What is your name?")


2 name = input() State .
3 print(f"Hello {name}")

Input / Output .

7
Walking through the code

1 print("What is your name?")


2 name = input() State .
3 print(f"Hello {name}")

Input / Output .

What is your name?

8
Walking through the code

1 print("What is your name?")


2 name = input() State .
3 print(f"Hello {name}")
name

Input/Output .

The name variable is initialised and What is your name?

assigned with the user input.

9
Walking through the code

1 print("What is your name?")


2 name = input() State .
3 print(f"Hello {name}")
name

Input/Output .

The name variable is initialised and What is your name?


Rebecca
assigned with the user input.

10
Walking through the code

1 print("What is your name?")


2 name = input() State .
3 print(f"Hello {name}")
name "Rebecca"

Input/Output .

The name variable is initialised and What is your name?


Rebecca
assigned with the user input.

11
Walking through the code

1 print("What is your name?")


2 name = input() State .
3 print(f"Hello {name}")
name "Rebecca"

Input/Output .

Hello is displayed on the screen with the text What is your name?
Rebecca
that was input and held in the name
Hello Rebecca
variable.

12
Types of data

Most programming languages will


“Passw0rd!
1
require a variable to be declared

before it is used.

Declaring a variable means to state


True
what type of data will be held by that
variable.

Python does not require this and works


5.2
a little differently.
“a”

13
Types of data

Python doesn’t require you to declare


“Passw0rd!
1
a variable. However, you still need to

be aware of data types because
incorrect data types can cause errors in
your programs. True

5.2

“a”

14
Types of data

There are five main data types that you


“Passw0rd!
1
need to be aware of:

● String (text)

True

5.2

“a”

15
Types of data

There are five main data types that you


“Passw0rd!
1
need to be aware of:

● String (text)
● Integer (whole numbers)
True

5.2

“a”

16
Types of data

There are five main data types that you


“Passw0rd!
1
need to be aware of:

● String (text)
● Integer (whole numbers)
True
● Boolean (True or False)

5.2

“a”

17
Types of data

There are five main data types that you


“Passw0rd!
1
need to be aware of:

● String (text)
● Integer (whole numbers)
True
● Boolean (True or False)
● Real or floating point numbers
(decimal numbers) 5.2

“a”

18
Types of data

There are five main data types that you


“Passw0rd!
1
need to be aware of:

● String (text)
● Integer (whole numbers)
True
● Boolean (True or False)
● Real or floating point numbers
(decimal numbers) 5.2
● Char (single string characters)
“a”

19
Types of data

Incorrect data types can cause 1 print("Enter a number")


problems during the execution of your 2 num1 = input()
3 print("Enter another number")
programs.
4 num2 = input()
Question . 5 print(num1+num2)

Predict what might happen when this


code is executed.

20
Types of data

The data type for an input is always 1 print("Enter a number")


string. When you add two pieces of 2 num1 = input()
3 print("Enter another number")
string together, it will concatenate
4 num2 = input()
(join) them. 5 print(num1+num2)
Instead of adding the two numbers
Enter a number
together to make 3, it has joined the
1
corresponding strings together to make Enter another number
12 (one,two). 2
12
This code has produced a logic error >>>
because it hasn’t executed as expected.

21
Types of data

If you want Python to use your value as 1 print("Enter a number")


an integer, then you need to tell it that 2 num1 = int(input())
3 print("Enter another number")
by casting the value.
4 num2 = int(input())
You do this by placing input() inside 5 print(num1+num2)
the int() function.
Enter a number
1
Enter another number
2
3
>>>

22
Types of data

input() and int() are both functions. 1 print("Enter a number")


2 num1 = int(input())
They are a type of subroutine that 3 print("Enter another number")
takes a value, processes it, and then 4 num2 = int(input())
returns another value back. 5 print(num1+num2)

Enter a number
1
Enter another number
2
3
>>>

23
Input, process, and output of input()

Input Process Output

The code written for the


function:
The user types The new value
● Takes the input
4 and presses is returned.
● Removes the extra
the Enter key.
line space
● Converts it to 4 is typically
4 is passed to held in a
string
the function. variable.
● Returns the new
value

24
Input, process, and output of int()

Input Process Output

The code written for the The new value


function: is returned.
The string ● Takes the string
value 4 is value 4 4 is typically
passed to the ● Converts it to an held in a
function. integer variable and
● Returns the new can now be
integer value 4 used for
calculations.

25
Types of data

Converting a value from one data type 1 print("Enter a number")


to another is known as casting. 2 num1 = int(input())
3 print("Enter another number")
4 num2 = int(input())
5 print(num1+num2)

26
Types of data

Errors can still happen during 1 print("Enter a number")


execution, even when casting has been 2 num1 = int(input())
3 print("Enter another number")
used.
4 num2 = int(input())
Question . 5 print(num1+num2)

What might happen if the user enters


‘four’ when this code is executed?

27
Types of data

You can avoid this type of error by 1 print("Enter a number")


2 try:
introducing validation checks.
3 number = int(input())
4 except ValueError:
Here is an example check that you can 5 print("You must enter a number")
use called try and except. 6 number = int(input())

You will learn more about these later on


in the course.

28
Types of data

To convert values to different data # convert to string


types, you need to know the functions str()
that are available to you.
# convert to integer
Here are the most common functions int()
that you will need to know.
# convert to real
You can find these in the Python
float()
documentation.

oaknat.uk/comp-pythonfunctions

29
Match the data types to their examples
Boolean “a”

String 5.2

Char “Hello”

Real/float 283

Integer False

30
Match the data types to their examples
Boolean “a”

String 5.2

Char “Hello”

Real / float 283

Integer False

31
Match the data types to their examples
Boolean “a”

String 5.2

Char “Hello”

Real / float 283

Integer False

32
Match the data types to their examples
Boolean “a”

String 5.2

Char “Hello”

Real / float 283

Integer False

33
Match the data types to their examples
Boolean “a”

String 5.2

Char “Hello”

Real / float 283

Integer False

34
Match the data types to their examples
Boolean “a”

String 5.2

Char “Hello”

Real / float 283

Integer False

35

You might also like