Skip to content
← Back to Community
Loops
Profile icon
MATTHEWBECHTEL

Does anyone now how to loop something?

Answered by ArchieMaclean [earned 5 cycles]
View Answer
Voters
Profile icon
ShuchiERUGUR
Profile icon
AnnaDale
Profile icon
TREVORCOHEN
Profile icon
AleciaMmor
Profile icon
RyleySpence
Profile icon
DonovanLam2
Profile icon
NatSparkes
Profile icon
DesmondBancroft
Profile icon
Julyru2304
Profile icon
DallasRollins
Comments
hotnewtop
Profile icon
ArchieMaclean

python:

# does 10 times for n in range(10): print(n) myarray = ["a","b","c"] # repeats for each item in the array for item in myarray: print(item) x = 0 # conditional loop while x<100: print(x) x+= 1 # loop forever (don't do this unless you know what you're doing, a # nd you're limiting how fast you can go through it) while True: print("oh dear, repl crashed")

Please upvote if this helps :)

Profile icon
MATTHEWBECHTEL

I will try it. Thanks. @ArchieMaclean

Profile icon
MATTHEWBECHTEL

Hm. I am not sure if it is perfect but definitely a nice start. Thank you. @ArchieMaclean

Profile icon
ArchieMaclean

@MATTHEWBECHTEL what do you mean? Is it not working for you?

Profile icon
MATTHEWBECHTEL

No it’s fine but my keyboard is lagging. Thanks. @ArchieMaclean

Profile icon
ArchieMaclean
Profile icon
MATTHEWBECHTEL

Is there a way to loop a certain Message though?

Profile icon
MATTHEWBECHTEL

Never mind, I fixed it. But do you think it will lag itself to death before it reaches 10k messages? @ArchieMaclean

Profile icon
Murzikal

@MATTHEWBECHTEL as in a print statement? then do

while True: print("your message")
Profile icon
ArchieMaclean

@MATTHEWBECHTEL If you want to do a giant loop, you can use xrange, like:

for n in xrange(100000): # do something

IDK what you mean by 'Message' though

Profile icon
DallasRollins

your mom

Profile icon
KieranCrockett

loops r awesome

Profile icon
MATTHEWBECHTEL

Ok one last question about loops. What’s the difference between range and xrange?

Profile icon
Murzikal

@MATTHEWBECHTEL range is python 3, xrange is py 2

Profile icon
Steven_The_GuyT

which language? I could probably give you an answer. Also, could you specify which type of loop? There are different loops, including for loops and while loops. Thank you!

Profile icon
PAULX
#this is repeating infinitely x = 2 while x > 1: print("Hi world.") #this is repeating 10 times x = 10 while x > 0: print("Hi world") x = x - 1
Profile icon
MATTHEWBECHTEL

Ok umm also is there a way to use Time.Sleep(1) but with a decimal as an time interval? For example: Time.Sleep(1.65) It would be super useful.

Profile icon
Geocube101

@MATTHEWBECHTEL It should work just as you typed it. Is it not working?

If not, try a lowercase 'T': time.sleep(value)

Profile icon
MATTHEWBECHTEL

No it is, but I’m just wondering if decimals work in time.sleep @Geocube101 like time.sleep(.89)

Profile icon
ArchieMaclean

@MATTHEWBECHTEL I think decimals work. Try it and see! If not,

import pygame pygame.time.wait(10) # waits 10 ms

may work (not sure if it works on just python repls though)

Profile icon
MATTHEWBECHTEL

Yes, I just realized that they work! Thank you. @ArchieMaclean

Profile icon
MATTHEWBECHTEL

Python.

Profile icon
ArchieMaclean

What language are you wanting to use?