Computer Science - 4CP0 PearsonEdexcel

Input/output

Overview

A program that never asks its user anything, and never saves anything, is not much use. Real programs collect information from the person using them, check that information makes sense before acting on it, and keep a permanent record on disk so the data survives after the program closes. That is what input, validation and file handling give you.

In this lesson you will learn how to accept input from the keyboard and respond to it correctly, how to build validation that rejects impossible or unusable values, and how to read from and write to a text file. Every example is written in Python, one of the languages you can use for the Paper 2 practical programming exam.

Objectives

  1. Understand how to write code that accepts and responds appropriately to user input
  2. Understand the need for, and how to implement, validation
  3. Be able to write code that reads/writes from/to a text file

Lesson Note

Every interactive program follows the same pattern: it displays a prompt, waits for the user to type something, does something with that value, and shows a result. In Python that pattern uses two built-in functions you will use constantly: input() to collect what the user typed, and print() to send output to the screen.

Available on the Green Bridge App

Download the Green Bridge CBT app on your phone or computer to access full lesson notes, practice questions, and more.

Full lesson notes with diagrams
AI-powered learning assistant
Study offline, anytime, anywhere
Available on Android, Windows, macOS, and Linux

Lesson Evaluation

Congratulations on completing the lesson on Input/output. Now that youve explored the key concepts and ideas, its time to put your knowledge to the test. This section offers a variety of practice questions designed to reinforce your understanding and help you gauge your grasp of the material.

You will encounter a mix of question types, including multiple-choice questions, short answer questions, and essay questions. Each question is thoughtfully crafted to assess different aspects of your knowledge and critical thinking skills.

Use this evaluation section as an opportunity to reinforce your understanding of the topic and to identify any areas where you may need additional study. Don't be discouraged by any challenges you encounter; instead, view them as opportunities for growth and improvement.

  1. What data type does Python's input() function always return? A) Integer B) Float C) String D) Boolean Answer: C
  2. Which file mode adds new content to a file without deleting what is already there? A) "r" B) "w" C) "a" D) "x" Answer: C
  3. A program must reject any exam mark that is not between 0 and 100. What type of validation check is this? A) Presence check B) Range check C) Length check D) Type check Answer: B
  4. Which statement should be used to open a file so that it closes automatically, even if an error occurs? A) import B) try C) with D) def Answer: C
  5. In Python, which method removes the trailing newline character from a line read from a file? A) .split() B) .strip() C) .read() D) .write() Answer: B

Available on the Green Bridge App

Download the Green Bridge CBT app on your phone or computer to access full lesson notes, practice questions, and more.

Full lesson notes with diagrams
AI-powered learning assistant
Study offline, anytime, anywhere
Available on Android, Windows, macOS, and Linux

Available on the Green Bridge App

Download the Green Bridge CBT app on your phone or computer to access full lesson notes, practice questions, and more.

Full lesson notes with diagrams
AI-powered learning assistant
Study offline, anytime, anywhere
Available on Android, Windows, macOS, and Linux

Practice Mock Questions

Want to practice mock questions on Input/output? Download the Green Bridge CBT app to access mock questions and full practice assessments for this topic.

Download The App On Google Playstore

Everything you need to excel in your exams

Green Bridge CBT Mobile App
Personalized AI Learning Chat Assistant
200,000+ Past Papers Across IGCSE, JAMB, WAEC & NECO
Over 3000 Lesson Notes
Offline Support - Learn Anytime, Anywhere
Green Bridge Timetable
Literature Summaries & Potential Questions
Track Your Performance & Progress
In-depth Explanations for Comprehensive Learning