MWZ

MINDWAREZONE

User Input in python

When writing Python programs, you often need information from the user. For example, you might ask for a name, age, or favorite color. Python makes this easy with the input() function.

In this tutorial, you'll learn how to take user input, work with different data types, and avoid common mistakes.

What is User Input?

User input is information entered by a user while a program is running. Python uses the input() function to collect this information.

Syntax:

input("Message")
            

The message inside the parentheses is displayed to the user, and Python waits for the user to type something and press Enter.

Taking Basic Input

Let's ask the user for their name.

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

Output:

Enter your name: John 
Hello, John
            

The value entered by the user is stored in the name variable.

Important: input() Always Returns a String

No matter what the user types, Python treats it as a string by default.

age = input("Enter your age: ") 
print(type(age))
            

Output:

<class 'str'>
            

Even if the user enters a number, the result is still a string.

Converting Input to an Integer

If you want to perform mathematical calculations, convert the input to an integer using int().

age = int(input("Enter your age: ")) 
print(age + 5)
            

Output:

Enter your age: 20 
25
            

Converting Input to a Float

Use float() when you need decimal values.

price = float(input("Enter product price: ")) 
print(price)
            

Output:

Enter product price: 99.99 
99.99
            

Taking Multiple Inputs

You can ask the user for more than one value.

name = input("Enter your name: ") 
city = input("Enter your city: ") 
print(name, "lives in", city)
            

Output:

Enter your name: John 
Enter your city: Delhi 
John lives in Delhi
            

Simple Calculator Example

num1 = int(input("Enter first number: ")) 
num2 = int(input("Enter second number: ")) 
sum = num1 + num2 
print("Sum:", sum)
            

Output:

Enter first number: 10 
Enter second number: 20 
Sum: 30
            

Getting Multiple Values in One Line

You can accept multiple values separated by spaces.

name, age = input("Enter name and age: ").split() 
print("Name:", name) 
print("Age:", age)
            

Output:

Enter name and age: John 25 
Name: John 
Age: 25
            

Many beginners try to add two inputs without converting them to numbers.

Common Mistake

Many beginners try to add two inputs without converting them to numbers.

num1 = input("Enter first number: ") 
num2 = input("Enter second number: ") 
print(num1 + num2)
            

Output:

Enter first number: 10 
Enter second number: 20 
1020
            

Python joins the strings instead of adding the numbers.

Correct Way:

num1 = int(input("Enter first number: ")) 
num2 = int(input("Enter second number: ")) 
print(num1 + num2)
            

Output:

30
            

Summary

  • Use the input() function to take user input.
  • input() always returns a string.
  • Use int() to convert input into an integer.
  • Use float() to convert input into a decimal number.
  • User input makes programs interactive and useful.
  • Always convert input before performing mathematical operations.