python input loop code example

Example 1: how to take input until a condition is satisfied in python

while True:
    try:
        # Note: Python 2.x users should use raw_input, the equivalent of 3.x's input
        age = int(input("Please enter your age: "))
    except ValueError:
        print("Sorry, I didn't understand that.")
        #better try again... Return to the start of the loop
        continue
    else:
        #age was successfully parsed!
        #we're ready to exit the loop.
        break
if age >= 18: 
    print("You are able to vote in the United States!")
else:
    print("You are not able to vote in the United States.")

Example 2: how to make inputs in a loop in python

for i in range(1,6): 
    globals()['string%s' % i] = input("Enter something: ")
    
#You can change how many times the loop iterates

print(string3)#This will return whatever was inputed
			  #in the third iteration of the loop

Example 3: taking input in for loop in python

n, m = list(map(int, input().split()))
arr = []
for _ in range(n):
    l = list(map(int, input().split()))[:m]
    arr.append(l)