-2

How can I make a while loop print something blank amount of times based on the user input. For example:

age = int(input('Enter your age: '))

while age…

And I just don’t know what to do from there. To be clear, how could I output something 8 times if the user inputs 8 (6 times if the user inputs six…)?

Edit: I want to run a while loop based on the USER INPUT, if they enter 3, it will print something 3 times. I don’t know what they will enter.

  • 1
    Does this answer your question? [for or while loop to do something n times](https://stackoverflow.com/questions/17647907/for-or-while-loop-to-do-something-n-times) – mkrieger1 Oct 26 '22 at 22:26
  • If you want to do something x number of times, use `for _ in range(x)`. – John Gordon Oct 26 '22 at 22:37
  • So I can't comment unless it's a fully fleshed, bulletproof, Congress-approved answer? Good luck with that attitude. – John Gordon Oct 26 '22 at 23:41
  • I am fully aware that my comment was not a full answer. That's why I posted it as a _comment_, not an _answer_. – John Gordon Oct 27 '22 at 19:50

2 Answers2

1

With a while-loop you have to use a condition for how long it will execute. To keep the value assigned to age use a counter variable like so:

age = int(input("Enter your age: "))

n = 0
while n < age:
    # Do something n-times as restricted by age
    n += 1
Wolric
  • 701
  • 1
  • 2
  • 18
1

One way you could do it is:

x = input(“What’s your age?”)

for x in range(x):

     #do something (x) times

This will ensure that the amount of times the program will do something x will be up to user input. Hope this helps!

astqx
  • 2,058
  • 1
  • 10
  • 21
Boss
  • 26
  • 6