Y10-01-CT4:
Input, integers and
debugging
Y10-01-CT4: Input, integers and debugging
Learning objectives
In this lesson students will learn to:
• take input and create output
• define the term 'runtime error’
• find and fix runtime errors
• use primitive data types (int, real, char, string).
For more information on this topic, and additional student activities,
refer to student book Topic 1.3.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT4: Input, integers and debugging
Making programs responsive
So far, you’ve been writing code that simply performs an action or
calculation.
However, for programs to be truly useful, people need to be able to
interact with them when they are running.
This is where the input() function comes in.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT4: Input, integers and debugging
Input
Let’s have a look at how input() works using an example.
name = "Eric"
Here is a program we have used previously: print(name)
To make it more useful, we want the user to type in their name.
To do this, we use the input function like this:
name = input("Enter your name: ")
print(name)
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT4: Input, integers and debugging
The input() function
This is the variable This is the input
where the data will be prompt. You should
stored. Remember to use it to give
make its name useful. instructions to the user.
name = input("Enter your name: ")
The = is the
assignment symbol. This is the function.
It means ‘is given the Be careful with
value of’. You’ve spelling and use
used it before. lower case letters.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT4: Input, integers and debugging
Input() and data types
By default, the input() function gives back a string.
We can see this if we inspect the data type of the variable.
This is fine if you’re just working with text, but what if you want
to work with other data types?
Type conversion is the answer. Python contains functions that
allow you to change the data type of the variable.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT4: Input, integers and debugging
Type conversion
What do you think each of these functions do?
int() Convert to integer
str() Convert to string
float() Convert to floating point number (real)
bool() Convert to Boolean
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT4: Input, integers and debugging
The input() function and type conversion
This is the variable
where the data will
be stored.
age = int(input("Enter your age in years: "))
The int is the type conversion. Notice
how the original input() function is
nested inside another set of brackets.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT4: Input, integers and debugging
Runtime errors
height = int(input("Enter height: "))
width = int(input("Enter width : "))
area = height * width
print(area)
Enter height: 5
Enter width : five
Traceback (most recent call last):
File "C:/Users/T/PycharmProjects/demos/[Link]", line 2, in <module>
width = int(input("Enter width : "))
ValueError: invalid literal for int() with base 10: 'five'
The problem with type conversion functions is that if you type in
something unexpected, your program will crash.
This is called a runtime error. It’s an error that occurs whilst a
program is running. There are a few different situations that cause
runtime errors. We’ll come across more of them as we develop more
complex code.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT4: Input, integers and debugging
Creating output
Sometimes you need to output more than one value on a line.
You can still use print() to do this.
height = int(input("Enter height: "))
width = int(input("Enter width : ")) Enter height: 4
area = height * width Enter width : 2
print("The area is", area) The area is 8
Use a comma to separate the string (which is inside quotes) and the
variable.
You can do this multiple times in a single line if necessary.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT4: Input, integers and debugging
Wrap up: you have learned how to…
Take input and create output.
• Using input() and print() to make a responsive program.
Define the term 'runtime error’.
• An error that occurs while the program is executing. They are
sometimes caused when a user enters an unexpected value.
Find and fix runtime errors.
• In our programs they were caused by our type conversions.
Use primitive data types.
• int, real, char, string.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.