Skip to content

Instantly share code, notes, and snippets.

@insipx
Created December 28, 2014 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save insipx/0e28a0ee318f40e15182 to your computer and use it in GitHub Desktop.
Save insipx/0e28a0ee318f40e15182 to your computer and use it in GitHub Desktop.
import random
def genName():
boyNames = {0: "Jack",1: "Andrew", 2: "Mike",3: "Terry",4: "Torvald", 5: "Gatsby"}
girlNames = {0: "Alice", 1: "Hana", 2: "Clare", 3: "Janet", 4: "Daisy"}
genderList = {0: "M", 1: "F"}
randGender = random.randrange(0, 2)
randGirlName = random.randrange(0, len(girlNames))
randBoyName = random.randrange(0, len(boyNames))
askGender = raw_input("What Name Gender would you like? (m/f) (enter 'r' for random)")
askGender = askGender.lower()
if askGender != "m":
if askGender != "r":
if askGender !="f":
print "please enter 'M' ,'F', or 'R' to initiate the Random Name Generator"
if askGender == "r":
if genderList[randGender] == "m":
return boyNames[randBoyName]
else:
return girlNames[randGirlName]
if askGender == "m":
return boyNames[randBoyName]
elif askGender == "f":
return girlNames[randGirlName]
print "Welcome to the Simple Random Name Generator by Liquid Think!"
print genName()
@mossbanay
Copy link

Rather than bothering with selecting an element by a random index from random.randrange, you can use random.choice(), for example:

if gender == "male":
    return random.choice(boyNames)
elif gender == "female":
    return random.choice(girlNames)

That omits the sample method johnzeringue mentioned above and arguably adds to the readability of the code.
Hope I could help!

@johnzeringue
Copy link

leaen, I knew about about random.choice at the time of writing but wanted to keep my post about coding instead of Python libraries.

Thanks for adding that, though. It's good to know what's already been written for you when you progress to larger projects.

@insipx
Copy link
Author

insipx commented Jan 31, 2015

Thank all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment