Talking to the program
After this: Ask for a line of text, turn it into a number when needed, and answer.
On this page · 4 sections · 3 checks
Every program so far had its values written inside it. To greet a different person, you changed the code. A program that can greet anyone has to ask: who are you?
Programs ask all the time. A quiz asks for your answer, a shop asks how many you want, and a game asks for your move. This lesson is about asking a question, and about what comes back.
Asking a question
input()keyboard.nextLine() makes the program stop and wait. The person running it types a line and presses Enter. What they typed comes back to the program as text, and you keep it in a box like any other value.
The smallest example asks for a name and says hello:
- The program prints the question:
What is your name? - It waits. The person types
Anaand presses Enter. - The box
namenow holds the text"Ana". - The program joins
"Hello, "and the name, and printsHello, Ana.
Step through it below. The typed line does not appear in the output panel; watch the box name instead.
- Run the programRun the program and step through it. The question is printed, the program waits, and the box name appears holding the typed text Ana.
print("What is your name?")name = input()print("Hello, " + name)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Python runs in the browser when you press Run on the block below. Java does not run here: if you are learning Java, step through the trace above instead, and paste the code into a Java tool on your computer when you want to run it for real.
The Input box under the code holds the lines the person types, one line per question. When the program runs, the Run block shows each typed line in the output after its question, as a terminal would.
Change Ana in the Input box to your own name and run it again.
It is always text
Whatever the person types comes back as text, even when they type digits. Type 41, and the box holds the text "41", not the number 41. "Your first program" showed what + does when one side is text:
text = input()print(text + 1)TypeError: can only concatenate str (not "int") to str
import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String text = keyboard.nextLine(); System.out.println(text + 1); }}411
Python stops with an error: it cannot add a number to text.Java does not stop. It treats the 1 as one more character and joins it on: 411. Either way, there is no 42.
The fix is the one from "Your first program": convert the text to a number with intInteger.parseInt before doing arithmetic with it.
- The typed line
41goes into the boxtext, as text. - int(text)Integer.parseInt(text) turns it into the number 41, which goes into the box
number. number + 1is 42, and the program prints it.
The trace band shows what kind of value each box holds. Compare text and number: same digits, different kinds.
print("Type a number:")text = input()number = int(text)print(number + 1)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Check yourself
The program asks for a number. The person types 41 and presses Enter. What does input()keyboard.nextLine() give back?
The person types 20 and presses Enter. What does the program print, not counting what the person typed?
print("Age?")age = int(input())print(age + 1)import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Age?"); int age = Integer.parseInt(keyboard.nextLine()); System.out.println(age + 1); }}Complete the missing line so that the program waits for a typed line and keeps it in the box name. With Ana typed, the program prints Hello, Ana.
print("What is your name?")____________________ # ← put the typed line in the box nameprint("Hello, " + name)import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("What is your name?"); ____________________ // ← put the typed line in the box name System.out.println("Hello, " + name); }}Honesty notes
- Ask the person running the program for a line (input() in Python, keyboard.nextLine() through a Scanner in Java) and keep it in a box.
- Say that what comes back is always text, even when the person typed digits.
- Convert the text to a number (int in Python, Integer.parseInt in Java) before doing arithmetic with it.
- Catch the conversion error when the typed text is not a number, as Errors and debugging taught.