← Back to course
Code Crew G6+ / Week 02 / Wednesday
3/6
Week 02 Β· Variables and input

Wednesday

Asking questions with input
// Programs that remember and ask
⏱ about 30 min

Wednesday: Asking questions with input

Rocket wants a program that works out how many oxygen tanks a mission needs. Each crew member needs two tanks. He writes it, runs it, types 4, and the program says 44 tanks.

"Forty-four tanks for four people?" he says. "That would fill the whole launch pad."

Raven reads the code slowly. "What do you notice?" she asks. "The 4 came from the keyboard. Yesterday, keyboard text in quotes did strange things with plus and times."

Nova blinks. "Interesting," she says. "Ask the shell what type input gives you. I think you will find a clue."

Rocket types a test in the shell and sees the answer: str. Now he knows what to fix.

input() gives you a string

input("question") shows the question, waits for the user to type and press Enter, and gives back what they typed. It always gives back a string, even when the user types digits.

A string times a number repeats the string. So "4" * 2 is "44". That was the bug.

# tanks_bug.py (has a bug)
crew = input("How many crew members? ")
tanks = crew * 2
print("Oxygen tanks needed:", tanks)
How many crew members? 4
Oxygen tanks needed: 44

Converting with int() and float()

int() turns a string of digits into a whole number. float() turns a string into a decimal number. Wrap input() inside one of them when you need a number.

# tanks.py
crew = int(input("How many crew members? "))
tanks = crew * 2
print("Oxygen tanks needed:", tanks)

In the shell, the 4 after the question is what the user typed:

How many crew members? 4
Oxygen tanks needed: 8
In tanks_bug.py, the user types 3. What number of tanks does it show?
WHY THIS EXERCISEWithout int(), "3" * 2 repeats the text, giving 33.
INPUT AND TYPES
  • Read the question.
  • Tap your answer.
What type does input() always give back?
Which line gets a decimal number like 2.5 from the user?
At your computer
1. Type tanks_bug.py in a new file and save it.
2. Press F5. Click in the shell after the question, type 4 and press Enter.
3. Now fix it: wrap input(...) inside int( ). Save as tanks.py.
4. Run it again with 4. You should see 8.
5. Run it once more and type 10. Predict the answer first.
Tip: the shell waits silently for your answer. Click after the question before you type.

Practice: two questions, one answer

Programs often ask more than one question. Convert each answer as soon as you get it, so every variable already holds the right type.

# supplies.py
crew = int(input("Crew size: "))
days = int(input("Days on the mission: "))
bottles = crew * days * 3
print("Water bottles to pack:", bottles)
READ SUPPLIES.PY
  • Read the question.
  • Tap your answer.
The user types 2 and 3. How many bottles does it show?
What would happen if the user typed four instead of 4?
Which line asks a question and waits?
Crew 3, days 10. Bottles to pack?
Crew 1, days 7. Bottles to pack?
StatementTrue or false?
input() gives back a string even if the user types 42.?
int(input("Crew size: ")) gives back a whole number.?
float() turns the string "2.5" into the number 2.5.?
You only need int() on the last question a program asks.?
WHY THIS EXERCISEEach input() hands back text, so every answer you want to do math with needs int() or float().

Your programs can now ask questions and use the answers. Tomorrow you will build neat, readable messages with f-strings.

← Tuesday