levo

Problem Solving with Programming

Keyboard input, screen output and CSV files

  • 3 things to know
  • Paper 2
Start revising · £5/month

Overview

input() and output, opening, reading, writing and closing CSV files, readline() and readlines(), splitting and stripping lines, and formatting output.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

What is a CSV file, and how does Python open one for reading?

Key term

How can Python read a CSV file line by line, and how do readline() and readlines() differ?

Key term

What does name = input("Name: ") do when it runs?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What does name = input("Name: ") do when it runs?

    Model answer

    The prompt Name: appears and the program waits for the user to type. The typed line is stored in name as a string.

  2. 2

    How should a program respond when a user picks option 9 from a menu offering 1 to 3?

    Model answer

    The program should reject the choice with a clear message. The program should then ask again rather than crash or carry on.

  3. 3

    Define a CSV file.

    Model answer

    A CSV file is a text file that separates its values with commas. Each line of the file usually holds one record.

  4. 4

    How is a file opened for reading in Python?

    Model answer

    The call open("data.csv", "r") opens the file for reading. The call returns a file object that the program reads from.

  5. 5

    What is the difference between opening a file with "w" and with "a"?

    Model answer

    The mode "w" creates the file or wipes an existing one before writing. The mode "a" adds to the end and keeps what is already there.

  6. 6

    Explain why a file must be closed after use.

    Model answer

    The close() method finishes any writing and releases the file. Other programs, or later code, can then use the file safely.

Other ways to work this

The same content, four other ways.

Everything in your subscription

Runs, plus a mode for every other kind of session.

1Flash
2Imposter
3Fade
4Brain dump

Learn every subtopic in runs of four questions

Facts flash and fade, a wrong word hides in some of them, and a brain dump decides which questions are locked in.

Computational Thinking
Data
Computers
Networks

Cram the night before, weakest topic first

A quick scan finds your weakest topics. Recall each fact you missed three times in a row.

The check is 45 + 27 = 72.
denarybinaryhexadecimal

Blank Blitz consolidates the facts

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

✓What it iscovered
~Why it happenspartly there
How long it lastsnot there yet

Explain It tests your understanding

Explain a concept in your own words and see which of its points you covered.

A kibibyte (KiB) is 1,024 bytes. The figure 1,024 is 2¹⁰.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The faults are the mistakes students make most often.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Problem Solving with Programming, on a schedule.

Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.