1

I am trying to create a program on Python which shows a random number every 3 seconds for 30 seconds. I know how to do it the long way like this:

import random

print (random.randint(0,100))

from time import sleep

sleep(3) # Time in seconds

I did this ten times. Is there any way to make this process shorter?

4
  • What do you exactly mean by "make this process shorter"? Commented Mar 1, 2014 at 21:01
  • By making it shorter, I mean not having to write it all out 10 times. Commented Mar 1, 2014 at 21:07
  • Then the answers below may solve your problem. Read about loops in Python, they are essential in programming. Commented Mar 1, 2014 at 21:07
  • Thank You to everyone who posted an answer. I found they all worked. I really appreciate it. Thank You. Commented Mar 1, 2014 at 21:17

2 Answers 2

1

Use looping, which is designed for just such requirements:

import random
import time
for i in range(10):
    print(random.randint(0, 100))
    time.sleep(3)
Sign up to request clarification or add additional context in comments.

Comments

0
from time import sleep
for x in range(0,10):
    print (random.randint(0,100))
    sleep(3)

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.