'How can I loop a user Input and shows the number of loop it has done?

I have this loop that ask a user, and I want to print "Enter a number (1):". Then the number in the parenthesis will increment each successful loop.

getinput = True
list1 = []

while getinput:
    for i in range(25):
        numbers = int(input("Enter a number: "))
        list1.append(numbers)
    getinput = False


for i in range(25):
    numbers = int(input("Enter a number: "))
    list1.append(numbers)
    print("Unsorted numbers: ", list1)


Solution 1:[1]

You can make use of string format() method :

 for i in range(25):
    numbers = int(input("Enter a number {}: ".format(i+1)))
    list1.append(numbers)

o/p would be like:

Enter a number 1:

for each iteration the number would change according to "i"

Solution 2:[2]

The for loop looks good, however the while loop implementation you are doing has one extra layer of for loop that is unnecessary. For while loop you have to set an end condition, which is when this end condition occurs, the while loop will exit. Without a end condition, the while loop will be an infinite loop because it doesn't know when to stop. In this case, setting a counter and decrement the count by 1 every time you ask for an input and exit the loop when the count equals 0.

count = 25
while getinput:
    numbers = int(input("Enter a number: "))
    list1.append(numbers)
    count -= 1
    if count == 0:
        getinput = False

print(list1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TUSHAR
Solution 2 LINDAAA