I watched this all day and cannot understand why gender input is repeated, despite the fact that it is only called once. This is not part of the cycle that I can see.
I tried to add variables to work as a counter and tried to use the if statement only to start input if the counter variable is less than 1, but cannot figure it out.
Edit: Thanks to the excellent feedback here, I found out that get_full_name causes duplication of gender input in get_first_name, but now I run into problems when trying to output randomly generated first and middle names.
I decided to set setFirst, setMiddle and setLast variables as global, but then I get a NameError. I also tried to create a new function to display them, but that didn't work either. I tried adding an "I". (without quotes) either directly in function (), or in one of the approaches below.
I will show the error first, then the full code.
Error: Traceback (last last call):
File “ init .py”, line 100, in main ()
File “ init .py”, line 92, in main
print displayName (setFirst, setMiddle, setLast)
NameError: global name 'setFirst ' undefined
I also get name errors trying to combine setFirst, setMiddle and setLast into another variable for the full name.
Here is the code:
from os.path import abspath, join, dirname
import random
full_path = lambda filename: abspath(join(dirname(__file__), filename))
FILES = {
'first:male': full_path('dist.male.first'),
'first:female': full_path('dist.female.first'),
'last': full_path('dist.all.last'),
}
def get_name(filename):
selected = random.random() * 90
with open(filename) as name_file:
for line in name_file:
name, _, cummulative, _ = line.split()
if float(cummulative) > selected:
return name
def get_first_name(gender=None):
global determine
global setFirst
print ("First name... Enter 1 for Male, 2 for Female or 3 to be surprised! ")
determine = input()
if determine == 1:
gender = 'male'
if determine == 2:
gender = 'female'
if determine == 3:
print ("You want to be surprised!")
gender = random.choice(('male', 'female'))
return get_name(FILES['first:%s' % gender]).capitalize()
setFirst = get_first_name()
print setFirst + " "
def get_middle_name(gender=None):
global setMiddle
if determine == 1:
gender = 'male'
if determine == 2:
gender = 'female'
if determine == 3:
gender = random.choice(('male', 'female'))
return get_name(FILES['first:%s' % gender]).capitalize()
setMiddle = get_middle_name()
print setMiddle + " "
def get_last_name():
global setLast
return "Smith"
setLast = get_last_name()
print setLast
def get_full_name(gender=None):
return u"%s %s %s" % (get_first_name(gender), get_middle_name(gender), get_last_name())
def main():
f = open('output', 'a')
f.write(get_full_name() + '\n')
f.close()
if __name__ == "__main__":
main()
main() :
def main(setFirst, setMiddle, setLast):
- NameError , . ?
"import random", "None", , - . ?
setFirst = None
setMiddle = None
setLast = None
, , :
def displayName (setFirst, setMiddle, setLast):
if setFirst == None:
print ("Random Baby Name Generator")
else:
print setFirst
print setMiddle
print setLast
if setMiddle == None:
print ("Double check the middle name variable.")
if setLast == None:
print ("Double check the last name variable.")