'How can I make this repeat forever?

I have this code which generates random characters (it's a kind of terrain generator). I just can't get it to print forever. Here's the current code I have for it:

import random
print(' '.join([random.choice(['#','o','~','*']) for i in range(10000)]))

I tried to do this, but I got a SyntaxError.

import random
print(' '.join([random.choice(['#','o','~','*']) while True]))

How can I get this to repeat forever? I also want a 0.05 second delay in between the printing of each character.



Solution 1:[1]

Well, if you want a delay between the characters, you can't use join() because that will give you a single string instead of a "character generator".

And if you plan on using an "infinite" generator, you can't use join() either for that same reason.

How about this:

import random
import sys
import time
while True:
    print(random.choice("#o~*"), end="", flush=True) # Python 3.3 and up
    time.sleep(0.05)

Solution 2:[2]

You can try this simplified version of you code:

import random, sys, time
while True: sys.stdout.write(random.choice('#o~*') + ' '); time.sleep(0.05)

Solution 3:[3]

Here's a new approach.

Roll your own

import random
import time

class GenerateRandomChar(object):
    def __init__(self, charlist):
        self.charlist = charlist
    def __next__(self):
        return random.choice(self.charlist)

terrain_tiles = GenerateRandomChar("#o~*")
while True:
    print(next(terrain_tiles), end='')
    time.sleep(0.05)

Also if you're building a map, you may want to think about something like:

class Map(list):
    def __init__(self, terrain_tiles, WIDTH=12, HEIGHT=12):
        self.WIDTH = WIDTH
        self.HEIGHT = HEIGHT
        self.terrain_tiles = GenerateRandomChar(terrain_tiles)
        for y in range(self.HEIGHT):
            self.append([next(self.terrain_tiles) for x in range(self.WIDTH)])
    def __str__(self):
        return "\n".join([" ".join([tile for tile in row]) for row in self])

>>> map_ = Map("~o#*")
>>> print(map_)
o o o o o o ~ ~ # ~ ~ ~
# # # o o # o # o # * #
~ o # * ~ # o # * ~ # ~
o * o o o * # o # o # *
# * o # # # * o ~ * # #
# ~ o ~ * # # ~ o o ~ ~
~ * # # o ~ o * # # ~ o
# o * o o * o * ~ ~ o *
# # * * ~ ~ ~ * # * # o
o o ~ ~ # * ~ ~ * ~ * o
~ * ~ ~ * # * * ~ # o #
* o ~ # ~ # ~ ~ o o o o

Solution 4:[4]

You can't have a loop inside your print function, you must put your print function inside a loop, so for example:

while True:
    print random.choice(['#','o','~','*']),

Solution 5:[5]

Infinite iterator with random symbols:

import itertools
it = (random.choice(['#','o','~','*']) for i in itertools.repeat(""))

As some people have commented, join() won't help you because it generates the whole string before returning. You can try (Python 3):

[print(char, end="") for char in it] # not such a good idea

or

for char in it: print(char, end="")

Solution 6:[6]

You can use generators:

from itertools import repeat
import random
from time import sleep
import sys

gen = (random.choice(['#','o','~','*']) for _ in repeat(None))
for x in gen:
    print x,
    sys.stdout.flush()
    sleep(0.05)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Noctis Skytower
Solution 3 Community
Solution 4
Solution 5 franzwr
Solution 6