Taking Conditional User Inputs - Python
Last Updated :
10 Jul, 2025
Taking conditional user inputs means asking user for input and then checking it against certain conditions before accepting or processing it. This helps ensure that input is valid, meets specific rules or triggers different actions based on user's response. It's useful for creating interactive and error-free programs.
Example:
Below code takes user's age as input and uses an if-else condition to check if they are eligible (18 or older).
Python
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible")
else:
print("Not Eligible")
Output
Enter your age: 22
Eligible
There are different ways to take conditional input in Python depending on what is being checked. Let's look at the most common types:
1. Using if and else Conditions
It is the most basic form of conditional input. After receiving input from user, programmer uses an if statement to check a condition and provides different responses based on whether condition is true or false.
Example:
This program checks if entered number is positive or not using a simple if-else condition.
Python
num = int(input("Enter a number: "))
if num > 0:
print("Positive Number")
else:
print("Not Positive Number")
Output
Enter a number: 12
Positive Number
2. Using if, elif and else Conditions
This allows programmer to check multiple conditions. The program goes through each condition one by one and runs the first one that is true.
Example:
Below code takes marks as input and uses if-elif-else conditions to assign a grade based on the score.
Python
marks = int(input("Enter your marks: "))
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 60:
print("Grade C")
else:
print("Grade D")
Output
Enter your marks: 98
Grade A
3. Using Nested if Conditions
Nested if conditions are used when a decision depends on multiple layers of checks. After first condition is true, program checks another condition inside it. This helps handle more detailed or specific user input situations.
Example:
Here, program checks the username and password using nested if statements and prints a login result based on input.
Python
username = input("Enter username: ")
password = input("Enter password: ")
if username == "admin":
if password == "1234":
print("Login successful.")
else:
print("Incorrect password.")
else:
print("Unknown username.")
Output
Enter username: admin
Enter password: 1234
Login successful.
When a program needs to ask user for input multiple times especially until they give a valid response a while loop is very useful. It keeps running and asking for input until a certain condition is met.
Example:
This program keeps asking user to enter correct password using a while loop. It only stops when user types "python123", then it prints "Access granted!".
Python
password = ""
while password != "python123":
password = input("Enter the password: ")
print("Access granted!")
Output
Enter the password: python
Enter the password: python1
Enter the password: python123
Access granted!
try-except helps handle input errors. If the user enters wrong type (like text instead of a number), try-except catches error and lets the program continue without crashing.
Example:
In this Example, a while loop is used with try-except to safely take user's age. It keeps asking until a valid number is entered preventing program from crashing on invalid input.
Python
while True:
try:
age = int(input("Enter your age: "))
break
except ValueError:
print("Invalid input! Please enter a number.")
print("Thank you! You entered:", age)
Output
Enter your age: abc
Invalid input! Please enter a number.
Enter your age: 18
Thank you! You entered: 18
Related Articles: