I need help trying to understand why this function returns forenames surnames instead of names. It's the first for loop that's got me confused I believe. For some background, the paths in the first for loop are just text files that list different names, one per line. Why are we returning forenames and surnames when each name from the files are being appended into the names variable? Please help, and let me know if more information is needed.
import random
def get_forenames_and_surnames():
forenames = []
surnames = []
for names, filename in ((forenames, "/home/mmelv/eclipse-
workspace/py30eg/data/forenames.txt"),
(surnames, "/home/mmelv/eclipse-
workspace/py30eg/data/surnames.txt")):
with open(filename, encoding="utf8") as file:
for name in file:
names.append(name.rstrip())
return forenames, surnames
forenames, surnames = get_forenames_and_surnames()
with open("test-names1.txt", "w", encoding="utf8") as file:
for i in range(100):
line = "{0} {1}\n".format(random.choice(forenames),
random.choice(surnames))
file.write(line)
The file for with list of forenames looks like:
Ailish
Cayden
Cohen
Maison
Becky
Dodou
Nickie
Zachary
Climate
The file with a list of surnames is the same but with different names.