Introduction
User input is an important part of many programs, allowing users to interact with the program and provide data at runtime. Python makes it easy to get input from users using the input()
function. In this chapter, you will learn the basics of user input, including getting input, converting it to different types, handling errors, and working with multiple inputs through registration, login, and calculator examples.
1. Getting User Input
The input()
function is used to get input from the user. It reads a line of text entered by the user, converts it to a string, and returns it.
Example
# Getting user input
name = input("Enter your name: ")
print("Hello, " + name + "!")
Output
Enter your name: Ramesh
Hello, Ramesh!
2. Converting Input Types
The input()
function always returns a string. If you need to work with other types of data, like numbers, you need to convert the input.
Example
# Getting integer input
age = input("Enter your age: ")
age = int(age) # Convert string to integer
print("You are " + str(age) + " years old.")
# Getting float input
height = input("Enter your height in meters: ")
height = float(height) # Convert string to float
print("Your height is " + str(height) + " meters.")
Output
Enter your age: 25
You are 25 years old.
Enter your height in meters: 1.75
Your height is 1.75 meters.
3. Handling Errors
When converting input to other types, errors can happen if the user enters invalid data. You can handle these errors using try
and except
blocks.
Example
# Handling errors
try:
age = int(input("Enter your age: "))
print("You are " + str(age) + " years old.")
except ValueError:
print("Invalid input. Please enter a valid number.")
Output
Enter your age: twenty
Invalid input. Please enter a valid number.
4. Registration and Login Program
Let’s create a simple registration and login program to demonstrate how to handle multiple inputs.
Registration
In the registration part, we will ask the user to enter a username, password, and email. We will then store these values.
Example
# Registration
username = input("Register a username: ")
password = input("Register a password: ")
email = input("Register your email: ")
print("Registration successful!")
Output
Register a username: Ramesh
Register a password: mypassword
Register your email: ramesh@example.com
Registration successful!
Login
In the login part, we will ask the user to enter the username and password again. We will check if they match the registered values.
Example
# Login
login_username = input("Enter your username: ")
login_password = input("Enter your password: ")
if login_username == username and login_password == password:
print("Login successful! Welcome, " + login_username + "!")
else:
print("Login failed. Invalid username or password.")
Output
Enter your username: Ramesh
Enter your password: mypassword
Login successful! Welcome, Ramesh!
Full Program
# Registration
username = input("Register a username: ")
password = input("Register a password: ")
email = input("Register your email: ")
print("Registration successful!")
# Login
login_username = input("Enter your username: ")
login_password = input("Enter your password: ")
if login_username == username and login_password == password:
print("Login successful! Welcome, " + login_username + "!")
else:
print("Login failed. Invalid username or password.")
5. Calculator Program
Let’s create a simple calculator program that can perform addition, subtraction, multiplication, and division based on user input.
Example
# Simple calculator
try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operation = input("Enter operation (+, -, *, /): ")
if operation == '+':
result = num1 + num2
elif operation == '-':
result = num1 - num2
elif operation == '*':
result = num1 * num2
elif operation == '/':
if num2 != 0:
result = num1 / num2
else:
result = "Error! Division by zero."
else:
result = "Invalid operation"
print("Result:", result)
except ValueError:
print("Invalid input. Please enter valid numbers.")
Output
Enter first number: 10
Enter second number: 5
Enter operation (+, -, *, /): /
Result: 2.0
6. Conclusion
Handling user input is a fundamental part of creating interactive programs. Using the input()
function, converting input types, and handling errors are key skills for any Python programmer. This tutorial covered the basics and included practical examples of a registration and login system and a calculator program to demonstrate handling multiple inputs effectively.