I have code that reads from files of surnames, female and male names and randomly generates a name in the following format: lastname, male/female name.
The files are in the following format:
JAMES 3.318 4,840,833 1
I have the following working code:
def picks(filename):
with open(filename) as f:
lines = [line.strip('\n') for line in f]
pick = randint(0, len(lines))
return lines[pick].split()[0]
def random_name(number_of_names):
nselect = ''
for _ in range(1, number_of_names+1):
g = randint(0,1)
if g==1:
nselect = picks('femalenames.txt')
else:
nselect = picks('malenames.txt')
print(g)
print( '{0:s}, {1:s}\n'.format(picks('surnames.txt'), nselect))
random_name(5)
The code generates the correct output but I feel the code could be trimmed a bit.