Input Integers and Debugging
Input Integers and Debugging
Learning objectives
In this lesson students will learn to:
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT4: Input, integers and debugging
© 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)
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT4: Input, integers and debugging
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
This is fine if you’re just working with text, but what if you want
to work with other data types?
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT4: Input, integers and debugging
Type conversion
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT4: Input, integers and debugging
© 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/demo1.py", line 2, in <module>
width = int(input("Enter width : "))
ValueError: invalid literal for int() with base 10: 'five'
Creating output
Sometimes you need to output more than one value on a line.
You can still use print() to do this.
Use a comma to separate the string (which is inside quotes) and the
variable.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT4: Input, integers and debugging
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.