Bird
Raised Fist0
Pythonprogramming~10 mins

Taking input using input() in Python - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to take input from the user and store it in a variable named name.

Python
name = [1]("Enter your name: ")
Drag options to blanks, or click blank then click option'
Aprint
Bint
Cinput
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of input.
Trying to convert input to int without calling input first.
2fill in blank
medium

Complete the code to take an integer input from the user and store it in variable age.

Python
age = [1](input("Enter your age: "))
Drag options to blanks, or click blank then click option'
Astr
Bint
Cfloat
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Not converting input to int and using string directly.
Using float instead of int.
3fill in blank
hard

Fix the error in the code to correctly take two numbers as input and add them.

Python
num1 = int(input("Enter first number: "))
num2 = int([1]("Enter second number: "))
result = num1 + num2
print(result)
Drag options to blanks, or click blank then click option'
Ainput
Bprint
Cint
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to convert print to int.
Missing input() call.
4fill in blank
hard

Fill both blanks to take two inputs from the user and print their sum.

Python
a = int([1]("Enter first number: "))
b = int([2]("Enter second number: "))
print(a + b)
Drag options to blanks, or click blank then click option'
Ainput
Bprint
Cint
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of input.
Trying to convert without reading input.
5fill in blank
hard

Fill all three blanks to take a name and age from the user and print a greeting message.

Python
name = [1]("Enter your name: ")
age = [2]([3]("Enter your age: "))
print(f"Hello {name}, you are {age} years old.")
Drag options to blanks, or click blank then click option'
Ainput
Bint
Cprint
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of input.
Not converting age to int.

Practice

(1/5)
1. What does the input() function do in Python?
easy
A. It stops the program immediately.
B. It prints text on the screen.
C. It asks the user to type something and returns it as text.
D. It converts text to numbers automatically.

Solution

  1. Step 1: Understand the purpose of input()

    The input() function waits for the user to type something and press Enter.
  2. Step 2: Identify what input() returns

    It always returns the typed data as a string (text), not numbers or other types.
  3. Final Answer:

    It asks the user to type something and returns it as text. -> Option C
  4. Quick Check:

    input() returns text [OK]
Hint: Remember: input() always returns text, never numbers directly [OK]
Common Mistakes:
  • Thinking input() prints text
  • Assuming input() stops the program
  • Believing input() converts text to numbers automatically
2. Which of the following is the correct way to ask the user for their name using input()?
easy
A. name = input Enter your name:
B. name = input('Enter your name: ')
C. name = input['Enter your name:']
D. name = input{Enter your name:}

Solution

  1. Step 1: Check the syntax of input() with prompt

    The prompt must be inside parentheses and quotes, like input('prompt').
  2. Step 2: Identify the correct option

    Only name = input('Enter your name: ') uses parentheses and quotes correctly to show the prompt.
  3. Final Answer:

    name = input('Enter your name: ') -> Option B
  4. Quick Check:

    Prompt text inside parentheses and quotes [OK]
Hint: Use parentheses and quotes for prompt text in input() [OK]
Common Mistakes:
  • Missing parentheses around prompt
  • Using brackets or braces instead of parentheses
  • Not putting prompt text inside quotes
3. What will be the output of this code if the user types 25 when asked?
age = input('Enter your age: ')
print(age + 5)
medium
A. 30
B. 255
C. 5
D. TypeError

Solution

  1. Step 1: Understand input() returns a string

    The variable age will be the string '25', not the number 25.
  2. Step 2: Analyze the print statement

    age + 5 attempts to add a string and an integer. In Python, this causes a TypeError, but if the code was print(age + '5'), it would concatenate to '255'. Since the code is print(age + 5), it raises a TypeError.
  3. Final Answer:

    TypeError -> Option D
  4. Quick Check:

    String + int causes TypeError [OK]
Hint: input() returns text; convert to int before adding numbers [OK]
Common Mistakes:
  • Assuming input() returns number
  • Trying to add int to string without conversion
  • Expecting automatic type conversion
4. Find the error in this code snippet:
number = input('Enter a number: ')
result = number * 2
print(result)
medium
A. number is a string, multiplying repeats text instead of doubling number
B. input() should be replaced with print()
C. Missing parentheses in print statement
D. No error, code works fine

Solution

  1. Step 1: Identify the type of number

    The input() function returns a string, so number is text.
  2. Step 2: Understand what number * 2 does for strings

    Multiplying a string by 2 repeats it twice, so if user types '3', result is '33', not 6.
  3. Final Answer:

    number is a string, multiplying repeats text instead of doubling number -> Option A
  4. Quick Check:

    String * 2 repeats text, not math multiply [OK]
Hint: Convert input to int before math operations [OK]
Common Mistakes:
  • Assuming input returns number
  • Expecting string * 2 to double value
  • Ignoring type conversion
5. You want to ask the user for two numbers and print their sum. Which code correctly does this?
hard
A. a = int(input('First number: ')) b = int(input('Second number: ')) print(a + b)
B. a = input('First number: ') b = input('Second number: ') print(a + b)
C. a = input('First number: ') b = input('Second number: ') print(int(a) + b)
D. a = input('First number: ') b = int(input('Second number: ')) print(a + b)

Solution

  1. Step 1: Convert inputs to numbers

    To add numbers, convert both inputs from strings to integers using int().
  2. Step 2: Add the converted numbers

    Only a = int(input('First number: ')) b = int(input('Second number: ')) print(a + b) converts both inputs before adding, so it sums correctly.
  3. Final Answer:

    a = int(input('First number: ')) b = int(input('Second number: ')) print(a + b) -> Option A
  4. Quick Check:

    Convert both inputs to int before adding [OK]
Hint: Convert all inputs to int before math [OK]
Common Mistakes:
  • Adding strings instead of numbers
  • Converting only one input
  • Forgetting to convert inputs