0

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)
2
  • Welcome to Stack Overflow! Please take the tour. What have you already tried? Do you know how to use f-strings, str.format(), or even string concatenation? Cause we can't really teach you them here, that's outside the scope of this site. SO is not meant to teach you the language basics. Check out How to ask and answer homework questions. For reference: What topics can I ask about here? See also How to Ask. Commented Feb 23, 2022 at 4:48
  • To be clear, you already have a number that increments: i Commented Feb 23, 2022 at 4:51

2 Answers 2

2

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"

Sign up to request clarification or add additional context in comments.

1 Comment

OMG this is what I'm looking for. Thank you!
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.