Get User Input in Loop using Python
Last Updated :
28 Apr, 2025
In Python, for and while loops are used to iterate over a sequence of elements or to execute a block of code repeatedly. When it comes to user input, these loops can be used to prompt the user for input and process the input based on certain conditions. In this article, we will explore how to use for and while loops for user input in Python.
User Input and For Loops in Python
Below are some of the examples by which we can understand how we can use for loop for user input in Python:
Example 1: Taking User Input Using for Loop
In this example, the code prompts the user for an integer input, then iteratively takes that many additional integer inputs to form a list, printing the resulting list of numbers.
Python3
# Prompting the user for input
user_input = int(input())
# Splitting the input string into individual numbers
number_list = []
for i in range(user_input):
number_list.append(int(input()))
# Printing the list of numbers
print("List of numbers:", number_list)
Output:
5
1
2
3
4
5
List of numbers: [1, 2, 3, 4, 5]
Example 2: Printing Even Numbers Using for Loop and User Input
In this example, the code takes a series of numbers separated by spaces as user input, converts them to integers, identifies and extracts the even numbers from the input, and finally prints the list of those even numbers.
Python3
# the user to input a series of numbers separated by spaces
user_input = input()
# Split the input string into individual numbers and convert each to an integer
numbers = [int(num) for num in user_input.split()]
# Initialize an empty list to store even numbers
even_numbers = []
# Iterate through the numbers using a for loop
for num in numbers:
if num % 2 == 0:
even_numbers.append(num)
# Print the list of even numbers
print("Even numbers from the input:", even_numbers)
Output:
129 12 23 24 26 28 233 333
Even numbers from the input: [12, 24, 26, 28]
User Input and While Loops in Python
Below are some of the examples by which we can use while loops for user input in Python:
Taking User Input Using While Loop
In this example, the code initializes an empty list, prompts the user to enter numbers iteratively until the user inputs 'done,' converts each input to an integer, appends it to the list, and finally prints the resulting list of numbers.
Python3
# Initializing an empty list to store numbers
number_list = []
# Prompting the user to enter numbers
while True:
num = input("Enter a number (or 'done' to finish): ")
if num == 'done':
break
number_list.append(int(num))
# Printing the list of numbers
print("List of numbers:", number_list)
Output:
Enter a number (or 'done' to finish): 1
Enter a number (or 'done' to finish): 2
Enter a number (or 'done' to finish): 3
Enter a number (or 'done' to finish): 4
Enter a number (or 'done' to finish): done
List of numbers: [1, 2, 3, 4]
Similar Reads
How to Input a List in Python using For Loop Using a for loop to take list input is a simple and common method. It allows users to enter multiple values one by one, storing them in a list. This approach is flexible and works well when the number of inputs is known in advance.Letâs start with a basic way to input a list using a for loop in Pyth
2 min read
Python String Input Output In Python, input and output operations are fundamental for interacting with users and displaying results. The input() function is used to gather input from the user and the print() function is used to display output.Input operations in PythonPythonâs input() function allows us to get data from the u
3 min read
How to Take Array Input in Python Using NumPy NumPy is a powerful library in Python used for numerical computing. It provides an efficient way to work with arrays making operations on large datasets faster and easier. To take input for arrays in NumPy, you can use numpy.array. Taking Array Input Using numpy.array()The most simple way to create
3 min read
Loop Through a List using While Loop in Python In Python, the while loop is a versatile construct that allows you to repeatedly execute a block of code as long as a specified condition is true. When it comes to looping through a list, the while loop can be a handy alternative to the more commonly used for loop. In this article, we'll explore fou
3 min read
Input a comma separated string - Python Handling comma-separated input in Python involves taking user input like '1, 2, 3' and converting it into usable data types such as integers or floats. This is especially useful when dealing with multiple values entered in a single line. Let's explore different efficient methods to achieve this:Usin
3 min read
Find Length of String in Python In this article, we will learn how to find length of a string. Using the built-in function len() is the most efficient method. It returns the number of items in a container. Pythona = "geeks" print(len(a)) Output5 Using for loop and 'in' operatorA string can be iterated over, directly in a for loop.
2 min read
List As Input in Python in Single Line Python provides several ways to take a list as input in Python in a single line. Taking user input is a common task in Python programming, and when it comes to handling lists, there are several efficient ways to accomplish this in just a single line of code. In this article, we will explore four com
3 min read
How to Take List of Tuples as Input in Python? Lists of tuples are useful data structures in Python and commonly used when you need to group related elements together while maintaining immutability within each group. We may need to take input for a list of tuples from the user. This article will explore different methods to take a list of tuples
3 min read
Iterate over characters of a string in Python In this article, we will learn how to iterate over the characters of a string in Python. There are several methods to do this, but we will focus on the most efficient one. The simplest way is to use a loop. Letâs explore this approach.Using for loopThe simplest way to iterate over the characters in
2 min read
How to Emulate a Do-while loop in Python? We have given a list of strings and we need to emulate the list of strings using the Do-while loop and print the result. In this article, we will take a list of strings and then emulate it using a Do-while loop. Do while loop is a type of control looping statement that can run any statement until th
3 min read