Click for All Topics
input()
function ,it stops the program execution and waits for user response and after getting the input it starts the further execution of the program.
Syntax –
input(prompt)
Parameter –
‘ ’
.
Example 1: Basic Input
# Simple input and display
user_name = input("Enter your name: ")
print("Hello, " + user_name +" this is edSlash" + "!")
# Output -
# Enter your name: Harshil
# Hello Harshil, this is edSlash!
Example 2: Numeric Input and Conversion
# Taking age as input and performing a calculation
age_str = input("Enter your age: ")
age = int(age_str) # Convert the entered string to an integer
future_age = age + 10
print("In 10 years, you will be", future_age, "years old.")
# Output -
# Enter your age: 20
# In 10 years, you will be 30 years old.
Example 3: Conditional Input
# Using input in a conditional statement
response = input("Do you want to continue? (yes/no): ")
if response.lower() == 'yes':
print("Continuing...")
else:
print("Cancelled.")
# Output -
# Do you want to continue? (yes/no): no
# Cancelled.
Example 4: Multiple Inputs
# Taking multiple inputs and performing a calculation
length_str = input("Enter the length: ")
width_str = input("Enter the width: ")
length = float(length_str) # Convert the entered strings to floats
width = float(width_str)
area = length * width
print("The area is:", area)
# Output -
# Enter the length: 10
# Enter the width: 5
# The area is: 50.0
Example 5: Input Validation
# Handling invalid input with a try-except block
try:
quantity_str = input("Enter the quantity: ")
quantity = int(quantity_str)
if quantity > 0:
print("Valid input. Quantity:", quantity)
else:
print("Please enter a positive quantity.")
except ValueError:
print("Invalid input. Please enter a valid integer.")
# Output -
# Enter the quantity: 10
# Valid input. Quantity: 10
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India