Fantasy Name Generator¶
Your friend is writing a new fantasy novel. It takes place in the mythical land of Aetheria. However, they have a problem, they're struggling to write names for their characters. Help them out by writing a function to generate some fantasy names.
Below, I've provided three things:
- A list of
first_names
that would be fitting for a fantasy character in Aetheria. - A list of
last_names
that would be fitting for a fantasy character in Aetheria. - I've imported the function
randint
for you to help you choose random names from this list.
Assignment¶
Write a function, random_name
which chooses a random first name and a random last name and returns them as a String.
For example:
print(random_name())
"Thranduil Starfire"
Extra
- Add or change the names in the
first_names
andlast_names
list. - Add epithets and job titles (i.e. "Thranduil Starfire the Drunk Bard")
- Have
random_name
return a list of names instead of a single name. If called without arguments,random_name
should return a list with a single name. But if passed a number, it should return a list of that length. All names returned in the list MUST BE UNIQUE.
How randint
Works¶
Here's the documentation on the randint
function I've imported for you below:
randint(a, b) method of random.Random instance
Return random integer in range [a, b], including both end points.
If you'd like to read more about the random
module, take a look at the Python 3 Documentation.
In [45]:
Copied!
# Here is some code to get you started...
from random import randint
from random import choice
first_names = ["Arwen", "Elrond", "Galadriel", "Legolas", "Thranduil", "Eowyn", "Boromir", "Faramir", "Gandalf", "Celeborn"]
last_names = ["Stormrider", "Shadowhunter", "Moonwhisper", "Nightshade", "Dragonbane", "Ironfist", "Brightblade", "Windwalker", "Starfire", "the Bard"]
epithets = ["Drunk", "Whimsical", "Tall", "Small", "Singing"]
jobs = ["Bard", "Knight", "Wizard", "Druid"]
def random_name(times=1):
names = []
for i in range(times):
random_first_name = first_names[randint(0, len(first_names) - 1)]
random_last_name = last_names[randint(0, len(last_names) - 1)]
random_epithet = epithets[randint(0, len(epithets) - 1)]
random_job = choice(jobs)
random_name = f"{random_first_name} {random_last_name} the {random_epithet} {random_job}"
if random_name not in names:
names.append(random_name)
return names
random_name(5)
# Here is some code to get you started...
from random import randint
from random import choice
first_names = ["Arwen", "Elrond", "Galadriel", "Legolas", "Thranduil", "Eowyn", "Boromir", "Faramir", "Gandalf", "Celeborn"]
last_names = ["Stormrider", "Shadowhunter", "Moonwhisper", "Nightshade", "Dragonbane", "Ironfist", "Brightblade", "Windwalker", "Starfire", "the Bard"]
epithets = ["Drunk", "Whimsical", "Tall", "Small", "Singing"]
jobs = ["Bard", "Knight", "Wizard", "Druid"]
def random_name(times=1):
names = []
for i in range(times):
random_first_name = first_names[randint(0, len(first_names) - 1)]
random_last_name = last_names[randint(0, len(last_names) - 1)]
random_epithet = epithets[randint(0, len(epithets) - 1)]
random_job = choice(jobs)
random_name = f"{random_first_name} {random_last_name} the {random_epithet} {random_job}"
if random_name not in names:
names.append(random_name)
return names
random_name(5)
Out[45]:
['Gandalf Starfire the Whimsical Knight', 'Gandalf Moonwhisper the Small Druid', 'Faramir Windwalker the Small Bard', 'Arwen the Bard the Drunk Druid', 'Elrond Nightshade the Tall Druid']
In [27]:
Copied!
foo = "foo"
bar = 10
my_new_str = f"foo is {foo} bar is {bar}"
my_new_str
foo = "foo"
bar = 10
my_new_str = f"foo is {foo} bar is {bar}"
my_new_str
Out[27]:
'foo is foo bar is 10'
In [ ]:
Copied!