Skip to main content
Skip to content

Lesson 12 of 12

Input with pucho and a mini project

Ask the user questions with pucho, turn answers into numbers, and put the whole course together in a report-card project.

12 min

Asking a question

pucho(sawal) ("ask") shows the question and gives back the next line of input as text. On this site, input comes from the Input box under the editor: each line of that box answers one pucho, in order. When the lines run out, pucho returns khali, so your program can notice there is nothing more to read.

Expected output

Press Run to try it

? Aapka naam?Namaste, Arjun

Answers are text — convert them

Whatever the user types arrives as text, even if it looks like a number. Before doing maths, wrap it in sankhya(). Forgetting this is the most common input bug: "10" + 5 would print 105.

Expected output

Press Run to try it

? Pehla number?? Doosra number?10 + 5 = 15

Reading until the input ends

Because pucho returns khali when there is nothing left, a jab_tak loop can read any number of lines. Check for khali before using the value.

Expected output

Press Run to try it

Aapne 3 cheezein likhi: aam, kela, seb

Mini project: report card

Time to combine variables, conditions, loops, functions, lists and input. The program below reads a student's name and marks, then prints the total, the percentage and a grade. Change the input box and run it again — then try adding a "highest subject" line on your own.

Expected output

Press Run to try it

? Student ka naam?? Marks?? Marks?? Marks?? Marks?Report card: PriyaTotal: 247 / 300Percentage: 82Grade: A

Practice exercise

Read two lines of input — a name and an age — and print <name> agle saal <age + 1> ka hoga. With the input below the output is Dev agle saal 25 ka hoga.

Expected output

Dev agle saal 25 ka hoga

Test input (fed to pucho)

Dev
24
Open in playground
Show solution
solution.altf
rakho naam = pucho("Naam?");
rakho umar = sankhya(pucho("Umar?"));
bol(naam, "agle saal", umar + 1, "ka hoga");