Home > database >  While loop based on USER INPUT
While loop based on USER INPUT

Time:10-27

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.

CodePudding user response:

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

CodePudding user response:

One way you could do it is:

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

for x in range(x):

 #do something.

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

  • Related