[Tutor] 'Name Genereator' Question
Tesla Coil
tescoil@irtc.net
Wed, 03 Jan 2001 19:55:46 -0600
On 03 Jan 2001, Curtis Larsen wrote:
> What I'd like to have is something like "map" that can be used
> against a number of occurrences of a character in a string and
> replace it -- without using a lot of loops. [...] The kicker (to
> me) is how to call "randint" for each character replacement
> target in "name".
Okay, it looks like a circus performer spinning plates, but
here it's done in a nested loop with a couple conditionals:
# Create names based on a filter where "." equals a random
# consonant and "," is a random vowel. The first argument
# is the # of names to generate, the second is the filter, and
# other letters can be used (including spaces) to fix a certain
# character for that spot. Example: namegen.py 10 ".,..,."
# might generate ten names like "becker", while namegen.py 5
# "j,.. r,bb,t" would only create five names like "jack rabbit".
import random, string, sys
vowels = "aeiou"
cons = "bcdfghjklmnpqrstvwxyz"
count = int(sys.argv[1])
filter = sys.argv[2]
for names in range(count):
name = filter
for letter in range(len(name)):
name = name[1:len(name)]+name[0]
if name[0]==".":
name = cons[random.randint(0,20)]+name[1:len(name)]
if name[0]==",":
name = vowels[random.randint(0,4)]+name[1:len(name)]
print name