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).
Pythonage=int(input("Enter your age: "))ifage>=18:print("Eligible")else:print("Not Eligible")
Output
Enter your age: 22
Eligible
Types of Conditional User Inputs
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 anifstatement to check a condition and provides different responses based on whether condition istrueorfalse.
Example:
This program checks if entered number is positive or not using a simpleif-else condition.
Pythonnum=int(input("Enter a number: "))ifnum>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 istrue.
Example:
Below code takes marks as input and usesif-elif-else conditions to assign a grade based on the score.
Pythonmarks=int(input("Enter your marks: "))ifmarks>=90:print("Grade A")elifmarks>=75:print("Grade B")elifmarks>=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.
Pythonusername=input("Enter username: ")password=input("Enter password: ")ifusername=="admin":ifpassword=="1234":print("Login successful.")else:print("Incorrect password.")else:print("Unknown username.")
Output
Enter username: admin
Enter password: 1234
Login successful.
4. Using while Loop for Repeated Input
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!".
Pythonpassword=""whilepassword!="python123":password=input("Enter the password: ")print("Access granted!")
Output
Enter the password: python
Enter the password: python1
Enter the password: python123
Access granted!
5. Using try-except for Valid Input Type
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.
PythonwhileTrue:try:age=int(input("Enter your age: "))breakexceptValueError: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: