Created
December 28, 2014 13:33
-
-
Save insipx/0e28a0ee318f40e15182 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
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!
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.
Thank all!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great! Thanks for all the help--This is only the first challenge and I'm already learning more than I had hoped!
I'll make sure to update the post on my website, and keep in mind your suggestions for the future challenges.
Thanks Again!