Console input and output

Reading from the keyboard, printing, converting an input, formatting an output (separator, line ending, number of decimals) and checking an input with a loop.

  • 27 cards
  • 3 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link

A few cards from the deck

  • Standard input: what is it?

    Show the answer

    The stream a program reads its data from, connected by default to the console keyboard.

    In Python, input() reads a line from this stream and always returns it as a string, even if the user types a number.

  • What does this program print?

    ```python
    print('a', 'b', sep='-', end='!')
    print(f'{3.14159:.2f}')
    `

    • a-b!3.14
    • a b!3.14
    • a-b!3.14159
    • a-b3.14
    Show the answer

    a-b!3.14

    sep='-' puts a hyphen between a and b, and end='!' replaces the line break: the second print continues on the same line. .2f keeps two decimals.

  • In Python, whatever value the user types, the input function always returns a value of type ….

    Show the answer

    In Python, whatever value the user types, the input function always returns a value of type string.

    input() guesses nothing: typing 7 gives '7', not 7. The program must convert with int() or float().

  • Standard output: what is it?

    Show the answer

    The stream a program writes its results to, shown in the console by default.

    print writes the text of the values to it, then by default a line break: the program shows only what it was told to write, nothing more.

  • The user types 21. What does this program print?

    ```python
    n = input()
    print(n * 2)
    `

    • 42
    • 2121
    • TypeError
    • 21 21
    Show the answer

    2121

    n is the string '21': * repeats it twice. You needed int(input()) to get 42.