The worksheetDownload the PDF
Answers

F1.7 Input from the user

Programming basics · GCSE · OCR J277 2.2.1, AQA 8525 3.2.7, Edexcel 1CP2 6.4.1 · about 12 min

BugBotLab

What this lesson is about

Asking questions with input(), and why every answer is text.

Questions 5 marks in all

  1. [1 mark]What does the program do when it reaches name = input("Name? ")?

    1. AShows the prompt and waits for the user to type and press Enter
    2. BCarries on and fills in name later
    3. CPrints the value of name
    4. DAsks the robot for its name
    Answer: A. input stops the program, and the robot, until an answer is typed. The answer is stored in name.
  2. [1 mark]The user types 20 at far = input("How far? "). What is stored in far?

    1. AThe string "20"
    2. BThe integer 20
    3. CThe real number 20.0
    4. DNothing until it is printed
    Answer: A. input always gives back text, even when the text looks like a number.
  3. [1 mark]What does this program print?

    far = "20"
    print(far * 3)
    Answer:
    202020

    far holds text, and multiplying text repeats it. This is what happens when an input answer is used without converting it.

  4. [1 mark]Why leave a space at the end of the prompt, as in input("Name? ")?

    1. ASo the answer is not squashed against the question
    2. BPython needs it to work
    3. CIt makes the answer a string
    4. DIt stops the robot
    Answer: A. What the user types appears straight after the prompt, so the space keeps them apart.
  5. [1 mark]Which of these are inputs to a program?

    Tick every answer that is true.

    1. AA name typed at the keyboard
    2. BA distance reading from a sensor
    3. CText printed to the screen
    4. DA note played on the buzzer
    Answer: A, B. Inputs go into a program: the keyboard and sensors. Printing and playing a note are outputs.

The task: greet by name

Ask What is your name? , then print Hello <name>, I am BugBot using the answer, and turn the LED green. The task will answer Ada, but your program must work for any name, so do not type Ada into it.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

name = ...

The hint students can ask for: Ask for the name and keep it in a variable, then use that variable in what you print. Change the LED after the greeting.

A solution

from bugbot import *
connect()
name = input("What is your name? ")
print("Hello " + name + ", I am BugBot")
led("green")

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.