0

I have a program that uses a loop, the thing is though, how can a make that loop repeat only a given amount of times? Code example below.

while True:
    print "This should be reprinted only ten times"

How can I make this code repeat itself only ten, or any given amount of times?

1 Answer 1

3

You can do:

for i in range(10):
    print "This should be reprinted only ten times"

OR

i=0
while i < 10:
    print "This should be reprinted only ten times"
    i+=1

OR SIMPLY

print "This should be reprinted only ten times\n"*10

For a range and random number:

from random import randint
num = randint(5,10)
print "This should be reprinted only {} times\n".format(num)*num
Sign up to request clarification or add additional context in comments.

2 Comments

How could I make it so it reprints a random amount of times in a certain range?
@TheNotGoodAtCodeGuy, If my answer was helpful, would you mind accepting it? Thank you.

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.