/
babynamer.py
134 lines (105 loc) · 4.09 KB
/
babynamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#======================================#
# BabyNamer #
# by Randall Arms #
# github.com/randallarms/babynamer #
#======================================#
from random import randint
import os
# Greeting text
print("\n\n=========")
print("BABYNAMER")
print("=========")
print("Open source @ github.com/randallarms/babynamer")
# Get file location
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
names_location = os.path.join(__location__, "names/")
# Acceptable values
genders = ["female", "f", "male", "m", "girl", "girls", "boy", "boys"]
booleans = ["yes", "y", "no", "n", "true", "false"] # Multipurpose for all y/n queries
# Get acceptable years by directory names with male & female names files
years = []
contents = os.listdir(names_location)
for item in contents:
male_names_location = os.path.join(names_location, item + "/male.txt")
female_names_location = os.path.join(names_location, item + "/female.txt")
if os.path.isfile(male_names_location) and os.path.isfile(female_names_location):
years.append(item)
# Name generation
def name_gen(year, gender, middle_option):
# Check year selction
if not year in years:
print("\nError: Year not found! ")
return False
# Determine gender
if not gender in genders:
print("\nError: Gender not found! ")
return False
elif gender.lower() == "m" or gender.lower() == "male" or gender.lower() == "boy" or gender.lower() == "boys":
gender = "male"
else:
gender = "female"
# Determine if generating middle name
if not middle_option in booleans:
print("\nError: Middle name setting not recognized! ")
return False
elif middle_option.lower() == "y" or middle_option.lower() == "yes" or middle_option.lower() == "true":
middle_option = True
else:
middle_option = False
f_names = open(os.path.join(__location__, "names/" + year + "/" + gender + ".txt"), "r")
# Fill list of possible names from file
names = []
for line in f_names:
names.append(line)
f_names.close()
# Generate name
name_str = ""
name_str += names[randint(0, len(names)-1)].strip('\n')
if middle_option == True:
name_str += " " + names[randint(0, len(names)-1)].strip('\n')
return name_str
# Menu, prompts
def menu():
# Get the year
print("\nWhich year do you wish to draw popular baby names from? (" + years[0] + "-" + years[len(years)-1] + ")")
y = input("> ")
# Get the gender
print("\nWhich gender do you wish to draw popular baby names from? (boy/girl)")
g = input("> ")
# Get the middle name option
print("\nDo you want middle name suggestions, too? (y/n)")
m = input("> ")
# Generate the name
name = name_gen(y, g, m)
# Unknown option selection warning
if name == False:
print("\nThere was an error generating names using the prompts specified. ")
print("\nAvailable options can be found in the README file and on GitHub. ")
return True
else:
# Print the results
print("\nYour baby name: ")
print(name + "\n")
# Go again prompt
again = True
while again == True:
print("\nWant to generate a name with the same parameters? (y/n)")
a = input("> ");
if not a.lower() in booleans or a.lower() == "n" or a.lower() == "no" or a.lower() == "false":
again = False
return True
if a.lower() == "y" or a.lower() == "yes" or a.lower() == "true":
again = True
name = name_gen(y, g, m)
print("\nBaby name: ")
print(name + "\n")
return True
# Run the menu task, or end it, according to user selection
menu_task = menu()
while menu_task == True:
print("\nDo you wish to try again? (y/n)")
i = input("> ")
if i.lower() == "y" or i.lower() == "yes" or i.lower() == "true":
menu_task = menu()
else:
exit()