In this Python challenge, we are going to create two functions to generate the name of a Knight and their coat of arms.
Name Generator
Let’s first generate the name of our knight. To do so we will code a sequencing algorithm that will use:
- The random library to randomly pick a first name and a nickname from a list of possible first names and another list of nicknames.
- String concatenation to combine the first name and the nickname together to produce the full name of our knight.
We will code this algorithm inside a function called knightNameGenerator(). This functions will generate and return the name of our knight so that we can store it in a variable and use it later on in our code.
Let’s investigate the code of our knightNameGenerator() function:
import random def knightNameGenerator(): firstnames = ["Lancelot","Charles","Henry","William","James","Richard","Edward"] nicknames = ["Brave","Horrific","Courageous","Terrific","Fair","Conqueror","Victorious","Glorious","Invicible"] firstname = random.choice(firstnames) nickname = random.choice(nicknames) return firstname + " The " + nickname
We can then write a short algorithm to test our function. Here is the full Python code for you to review and test:
Coat of Arms Generator
In the medieval times, each knight had a coat of arms: symbol used to represent their family. Your task consists of generating a new function called generateCoatOfArms() that will randomely pick and combine a colour and an animal from two separate lists of possible values and combine them together (using string concatenation) to generate and return the coats of arms of both player 1 and player 2.
You will need to complete this code in the above trinket frame.
To help you with this code you may use the following two lists of possible values:
colours = ["Red","Golden","Blue","Purple","White","Silver"] animals = ["Lion","Dragon","Unicorn","Horse","Tiger"]
For instance, the output of your program could be:
- Player 1: Your name is William The Brave
- Your coat of arms is a Red Lion
- Player 2: Your name is Lancelot The Victorious
- Your coat of arms is a Golden Dragon
Extension Task 1:
Tweak your new generateCoatOfArms() function two generates two random colours to be used when generating your coat of arms. If both colours are different the output would be:
- Player 1: Your name is William The Brave
- Your coat of arms is a Red and White Lion
If both randomly picked colours are the same then, only one should be used to describe the coat of arms:
- Player 2: Your name is Lancelot The Victorious
- Your coat of arms is a Purple Unicorn
Extension Task 2:
Create one more function to randomly generate the favourite weapon of your knight e.g. iron mace, golden flail, silver axe, silver sword, wooden shield, etc.
- Your favourite weapon is an iron mace.
Add more functions to describe the top qualities of your knight or where they live e.g. haunted castle, hidden mansion, fortified tower, etc.
- You are loyal and combative
- You live in a haunted castle
Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area