levo

Problem Solving with Programming

Keyboard input, screen output and CSV files

  • 3 things to know
  • Paper 2

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.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Computational Thinking
Data
Computers
Networks

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

The check is 45 + 27 = 72.
denarybinaryhexadecimal

Blank Blitz makes facts automatic

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

AA normal test value is 14.
BA boundary test value is 16.
CAn erroneous test value is 20.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

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 traps are the mistakes students actually make.

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.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

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