Reputation: 701
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.
Upvotes: 8
Views: 3935
Reputation: 853
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','~','*']),
Upvotes: 3
Reputation: 54183
Here's a new approach.
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
Upvotes: 3
Reputation: 17670
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)
Upvotes: 2
Reputation: 336128
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)
Upvotes: 12
Reputation: 106
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="")
Upvotes: 2
Reputation: 21991
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)
Upvotes: 3