Python, properly · Chapter 5 of 42
Input and output
By the end of this chapter you can write programs that ask a question, turn the answer into a number, and answer bad input with a friendly line instead of a traceback. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: First steps
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.
Why you need this
Every program so far gave the same output every time: the values were written into the file. Bhavna wants something the counter staff can use while a customer waits: type how many rotis, see the price; type the distance, see how long the delivery takes. And the staff will type anything, so the program must not crash in front of a customer.
That needs three things: a way to ask, a way to turn the answer (which is always text) into a number, and a way to catch the answers that are not numbers.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
greet.py: greet any customer
The order counter asks for a name and greets the customer, however they typed it. Ask with the prompt Your name: (with a space after the colon), then print the welcome with the name cleaned up: no spaces at the ends, each word starting with a capital. When the customer types ravi kumar the screen shows:
Your name: ravi kumar
Welcome to DabbaDrop, Ravi Kumar!
The check types the name for you, so its output is one line: Your name: Welcome to DabbaDrop, Ravi Kumar!
- Task 2
rotis.py: price any number of rotis
A roti costs 15 rupees. Ask How many rotis? (with a space after the question mark), and print what they cost. When the customer types 3:
How many rotis? 3
3 rotis cost Rs 45
The check types the number for you (3, then 12, then 40), so its output is one line, like: How many rotis? 3 rotis cost Rs 45
- Task 3
eta.py: how long until it arrives
Bhavna tells customers how long a delivery takes. Ask Distance in km? and then Speed in km/h? (each with a space after the question mark). The distance can have a decimal point, the speed is a whole number. The time in minutes is the distance divided by the speed, times 60, rounded to a whole number with round. For 4.5 and 15:
Distance in km? 4.5
Speed in km/h? 15
Arrives in about 18 minutes
The check types both answers, so its output is one line: Distance in km? Speed in km/h? Arrives in about 18 minutes
- Task 4 · interview question
Interview: tiffins.py, a friendly answer to bad input
Customers type anything. Ask How many tiffins? (with a space after the question mark). For a whole number, print Order placed: and the number, then the word tiffins. For anything else (three, 2.5, or nothing at all), print a friendly line instead of a traceback, and the program ends normally. With 3, the check sees:
How many tiffins? Order placed: 3 tiffins
With three, it sees:
How many tiffins? Please type a whole number, like 3
What you will learn
- input(): asking a question and getting the typed answer.
- Why the answer is always text, and the bugs that causes.
- Converting: int(), float() and str().
- ValueError, and try / except for a friendly message.
More in First steps
Build "Input and output" yourself
DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.