Code / Python for Beginners / Unit 1: Hello, Python

Asking questions with input() input() से सवाल पूछना

Lesson 3 of 14 · about 14 minutes · Read what the user types; turn text into numbers with int() and float().

⚡ 0 XP

Step 1 of 2

input() waits for an answer

movemoveturnpick

input("Your name: ") shows a question and waits for the user to type an answer. Store the answer in a variable:

name = input("Your name: ")

Here the answers come from the Inputs box next to the editor, one answer per line. Change the text there and press Run again to try other answers.

If your program asks more questions than there are lines in the Inputs box, Python stops with an EOFError. Just add another line.

Example
city = input("Which city? ")
print("I love", city)

🎯 Challenge: Namaste, whoever you are +10 XP

This program always greets Asha. Change it so it asks Your name: with input() and greets whoever answers: Namaste, <name>!. With the answer Ravi it must print Namaste, Ravi!.

Try twice to unlock the solution (0/2)

main.pyCtrl ⏎
Output

What your program prints appears here.

Checklist

Press Run to check your code.

  • Greets the name from the Inputs box
  • Works for Ravi too
  • Uses input()
Lesson notes (text version)

1. input() waits for an answer

input("Your name: ") shows a question and waits for the user to type an answer. Store the answer in a variable:

name = input("Your name: ")

Here the answers come from the Inputs box next to the editor, one answer per line. Change the text there and press Run again to try other answers.

If your program asks more questions than there are lines in the Inputs box, Python stops with an EOFError. Just add another line.

                city = input("Which city? ")
print("I love", city)
              

Challenge: Namaste, whoever you are

This program always greets Asha. Change it so it asks Your name: with input() and greets whoever answers: Namaste, <name>!. With the answer Ravi it must print Namaste, Ravi!.

2. The int() trap

This is the number 1 beginner mistake in Python, so read slowly.

input() always gives you text (a str), even when the user types a number. If the user types 3, you get "3", not 3.

Text does strange things with maths:

  • "3" + "3" gives "33" (glued together)
  • "3" * 15 gives "333333333333333" (the text repeated 15 times!)

The fix: convert the text to a number before doing maths.

  • int("3") gives the whole number 3
  • float("2.5") gives the decimal 2.5

The common pattern is cups = int(input("How many cups? ")). Read it from the inside out: ask, then convert.

                cups = input("How many cups? ")
print(cups * 3)        # oops: text repeated
cups = int(cups)
print(cups * 3)        # real maths
              

Challenge: The chai bill

A cup of chai costs ₹15. The program asks how many cups, but the bill comes out crazy! Fix it with int() so that total is a number and the program prints Total: ₹45 for 3 cups (and Total: ₹60 for 4 cups).

Search by task, format or tool name.