Taking continuous input in Python means that the program keeps asking the user for input until a certain condition is met. This is typically achieved using loops. In this Python tutorial, I will show you an example of how to take continuous input in Python.
How to ask for user input in Python
Let us see, an example of how to ask for user input in Python.
In this example, I have taken two inputs as A = int(input(“enter 1st number”)), B = int(input(“enter 2nd number”)), and used the addition operation for the inputs.
Example:
A = int(input("enter 1st number"))
B = int(input("enter 2nd number"))
C = A + B
print(C)
To get the sum of inputs as output, we have to use print(C). The below screenshot shows the output.
How to take continuous input in Python
Here, we can see how to take continuous input in Python.
- In this example, I have taken input as age = int(input(“Enter age: “)) and the while loop. The while loop takes an expression and executes the loop body.
- The while true always evaluates the boolean value true and executes the body of the loop infinity times. The try and except is used, try is used to test the block of code for errors and except block is used to handle the errors.
- If the condition is true, it returns the if statement else it returns the else statement.
- The continue statement is used to end the present iteration and continue with the next iteration.
Example:
while True:
try:
age = int(input("Enter age: "))
if age<=20:
print("The age is correct")
break;
else:
print("The age is not correct")
except ValueError:
print("Invalid")
continue
The loop ends when the condition is true. I have used print(“The age is correct”) when the given condition is true. You can refer to the below screenshot for the output.
How to Take Continuous Input in Python – Another Example
Let us see another example of how to take continuous input in Python.
Step 1: Understand the Basic Input Function
Python has a built-in function called input()
which is used to take user input.
Example:
user_input = input("Enter something: ")
print("You entered:", user_input)
Step 2: Implement Looping for Continuous Input
You can use loops, such as while
loops, to keep taking input from the user until a certain condition is met.
Using While Loop
while True:
user_input = input("Enter something (or 'exit' to stop): ")
if user_input == 'exit':
break
print("You entered:", user_input)
This will keep taking input from the user until they enter ‘exit’.
Using Sentinel Values
Another common way to take continuous input is by using a sentinel value, which signals the program when to stop taking input.
user_input = ""
while user_input != "exit":
user_input = input("Enter something (or 'exit' to stop): ")
if user_input != 'exit':
print("You entered:", user_input)
This example also uses exit
as the sentinel value to break the loop.
Step 3: Input Validation
Sometimes, you might want to validate the user input and continue asking for input until you get a valid response.
Example (Taking an integer as input):
while True:
user_input = input("Enter a number (or 'exit' to stop): ")
if user_input == 'exit':
break
if user_input.isdigit():
print("You entered the number:", user_input)
else:
print("That's not a valid number. Try again.")
Step 4: Handling Multiple Inputs in a Single Line
Sometimes, you might want to accept multiple inputs in a single line separated by spaces.
Example:
while True:
user_input = input("Enter numbers separated by spaces (or 'exit' to stop): ")
if user_input == 'exit':
break
numbers = user_input.split()
if all(num.isdigit() for num in numbers):
print("You entered the numbers:", numbers)
else:
print("Please enter only numbers. Try again.")
Step 5: Error Handling
It’s good practice to handle exceptions, especially when converting user input into other data types.
Example (Converting input to an integer):
while True:
user_input = input("Enter an integer (or 'exit' to stop): ")
if user_input == 'exit':
break
try:
number = int(user_input)
print("You entered the integer:", number)
except ValueError:
print("That's not a valid integer. Try again.")
When you run the above code, you can see the output below:
How to ask for a number in Python
Let us check an example of how to ask for a number in Python.
In this example, I have taken the input as Number = int(input(“Enter a number”)). We have to use int datatype for the integer input.
Example:
Number = int(input("Enter a number"))
print("The Number is",Number)
To get the output, I have used print(“The Number is”,Number). You can refer to the below screenshot for the output.
This is how to ask for a number in Python.
Conclusion
In this tutorial, we have learned, how to ask for user input in Python as well as, how to ask for continuous input from users in Python.
You may also like:
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.