Reputation: 59
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)
Upvotes: 0
Views: 1948
Reputation: 36
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)
Upvotes: 1
Reputation: 891
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"
Upvotes: 2