Lesson 5 – User Input

Many programs need information from the user. Python allows this through the input() function. When your program reaches an input line, it waits for the user to type something on the keyboard. Whatever the user types is always received as text (a string), even if the user enters numbers.

How Does User Input Work?

When you write an input instruction, Python shows a message and waits. After the user types something and presses Enter, the value is stored in a variable.

Example 1: How do we ask for a name?

name = input("Enter your name: ")
print("Hello", name)
        

Explanation: The program asks the user to type a name. Whatever the user types becomes the value of the variable name.

Sample Output:

Enter your name: Ali
Hello Ali
        

Example 2: What happens if we input a number?

age = input("Enter your age: ")
print("You are", age, "years old.")
        

Explanation: Even if the user enters 15, it is stored as text, not a number.

Enter your age: 15
You are 15 years old.
        

Converting Input into Numbers

To perform calculations, the input must be converted using int() or float(). This tells Python to treat the input as a number instead of text.

Example 3: How do we convert input to an integer?

age = int(input("Enter your age: "))
print("Next year you will be", age + 1)
        
Enter your age: 20
Next year you will be 21
        

Example 4: How do we accept decimal numbers?

price = float(input("Enter the price: "))
print("Price with tax:", price + 2.5)
        
Enter the price: 10.5
Price with tax: 13.0
        

Example 5: Combining text and numbers

name = input("Enter your name: ")
marks = int(input("Enter your marks: "))

print(name, "scored", marks, "marks in the test.")
        
Enter your name: Sarah
Enter your marks: 88
Sarah scored 88 marks in the test.
        

Why is User Input Important?

User input makes your programs interactive. Without it, programs can only show fixed information. With user input, every result depends on what the user types, making programs more powerful.


Quiz: Test Your Understanding

1. What does the input() function do?

Prints a message
Waits for user input
Calculates numbers
Exits the program
Correct answer: Waits for user input

2. What type does input() return?

int
float
str
bool
Correct answer: str

3. Which function converts input to an integer?

toInt()
convert()
int()
float()
Correct answer: int()

4. Which input method is used for decimal values?

inputFloat()
float()
decimal()
num()
Correct answer: float()

5. Which of the following shows correct input use?

ask("Enter name")
print("Enter name: ")
input("Enter name: ")
name = "Enter name"
Correct answer: input("Enter name: ")

6. What will this produce? int("5")

"5"
5
Error
55
Correct answer: 5

7. What will input("Enter number: ") return?

A number
A string
A float
True
Correct answer: A string

8. What does this print? name = "Ali" print("Hello", name)

Hello name
Hello Ali
Ali Hello
name Hello
Correct answer: Hello Ali

9. What is wrong with this? age = input("Enter age: ") print(age + 5)

Nothing
Input is a string
age is an int
Python cannot print
Correct answer: Input is a string

10. Which code correctly converts input to a float?

number = input(float)
number = float(input("Enter: "))
number = input("float")
number = float
Correct answer: number = float(input("Enter: "))
Back to Top