Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Learn Input and Output

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

This lesson is

being
recorded
APPLY TASK ANSWER
WEEK 3
Starter

What code word would you need to ask the user to enter their name?

Theory Past Paper Mark Scheme Video


Starter

What code word would you need to ask the user to enter their name?

input

Theory Past Paper Mark Scheme Video


Edexcel iGCSE Computer
Science

2.4.1 Input & Output


Syllabus
Learning Objectives

Understand how to write code that accepts user input

Understand how to write code that responds to user input

Understand how to format both input and output statements


Textbook Reference

Although there are lots of resources available for this


qualification, your lessons refer to this textbook.
Pearson Edexcel International GCSE (9-1)
Computer Science Student Book
2.4.1 pages 34 - 58
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.


Input

Let’s have a look at how input() works using an example.

Here is a program: name =


"Eric"
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)
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.
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.
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
bool() (real)
Convert to Boolean
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.
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
")) Enter width : 2
area = height * width The area is 8
print("The area is", area)
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.


<string>.format()

In Python you can use the <string>.format() function to modify the look of a string when it is output.

The format function allows programmers to substitute variables into strings and add positional formatting.
It makes output more readable and user friendly .

Recall that the print() function displays one line of text to the screen (and also starts a new line).

Combined, these two functions can produce rows of output text that are formatted in the layout needed.
<string>.format() usage

Format works by putting placeholders into a string, defined by { }, and calling the <string>.format()
function.

Outputs:

You can do this with other data types and with multiple placeholders, like so:

Outputs:
<string>.format() usage

You can also specify which element you want to include in the place holder using an index value.

This code will output:

You can also add variable names to the items in the format function so that these are used in the
placeholder, like so:

This code will output:


<string>.format() usage

You can also specify the data type of a value, like so:

This code outputs.

Limit to two
decimal places
Let’s look closer at this:

Convert to float
Selects first
item in format
Plenary
Use the following summary and video link to make notes for your Apply task.

https://student.craigndave.org/videos/gcse-edexcel-topic-6b-user-input-and-display-output
Plenary
Use the following summary and video link to make notes for your Apply task.

https://student.craigndave.org/videos/gcse-edexcel-topic-6b-formatting-and-customising-string-output
Syllabus
APPLY TASK
Check your ‘to do’ list
for your Apply task
Deadline 1 week
Lesson complete!
See you next lesson

You might also like