In this Python Control Structures article we want to talk about Python Control Structures for Handling User Input, so handling user input is an important task in many programs. Python provides several control structures that makes it easy to handle user input and ensure that the input meets the program requirements. in this article we want to talk that how to use Python control structures to handle user input.
First of all let’s talk that how we can get user input in Python, so in Python you can use input() function to prompt the user for input. this function displays a message to the user and it waits for the user to enter input. after that the user has entered input, than the function returns input as a string.
For example we want to prompt the user for their name and after that we want to greet them. we can do this using this code.
1 2 |
name = input("Please enter your name: ") print("Hello " + name) |
Run the code and this will be the result

Handling User Input with Control Structures
After that we have user input, we often need to validate it and ensure that it meets our program requirements. Python provides different control structures to handle user input and ensure that it meets our requirements. these control structures include if statements, while loops and exception handling.
Python If Statements
Python if statements allows us to check if a condition is true or false. we can use if statements to validate the user input.
For example we want to prompt the user for a number between 1 and 10. we can use this code to validate user input.
1 2 3 4 5 6 |
num = input("Please enter a number between 1 and 10: ") if num.isdigit() and 1 <= int(num) <= 10: print("Thank you for entering a valid number.") else: print("Sorry, you did not enter a valid number.") |
Run the code and this will be the result

Python While Loops
Python while loops allows you to repeat a block of code as long as a condition is true. we can use while loop to prompt the user for input until they enter valid input.
For example we want to prompt the user for positive integer. we can use this code to prompt the user until they enter a valid input:
1 2 3 4 5 6 7 8 9 10 11 12 |
num = -1 while num < 0: num = input("Please enter positive integer: ") if num.isdigit(): num = int(num) else: print("Sorry you did not enter valid number.") num = -1 print("Thank you for entering valid number.") |
Run the code and this will be the result

Python Exception Handling
Python Exception handling allows us to handle errors that occur during program execution. we can use exception handling to handle user input errors and ensure that our program does not crash.
For example we want to prompt the user for a number and then divide it by 2. if the user enters non numeric value, our program will crash. for this we can use exception handling to handle this error and display an error message to the user.
1 2 3 4 5 6 7 8 9 10 |
while True: try: num = int(input("Please enter a number: ")) result = num / 2 print("Half of {} is {}".format(num, result)) break except ValueError: print("Sorry you did not enter a valid number") except ZeroDivisionError: print("Sorry cannot divide by zero.") |
Run the code and this will be the result
