Python Coding Question: Boardgame player generator. Write a program to generate
ID: 3724817 • Letter: P
Question
Python Coding Question:
Boardgame player generator. Write a program to generate a random player from the following specs:
Name: write a function randomName( ) that randomly chooses a first and last name and then concatenates and returns them.
First: Myrtis, Liz, Delmar, Regina, Loyd, Lavonia, Ed, Lelah, Shaneka, Penny
Last: Smith, Johnson, Williams, Jones, Brown, Davis, Miller, Wilson, Moore, Taylor
Class: write a function randomClass( ) that randomly chooses and returns the name of one of the following classes:
chef, rancher, scientist, teacher, taxi driver, janitor, medical doctor, carpenter, blacksmith, florist, engineer, farmer
Statistics: Each character will have six numeric base stats: STR (strength), CON (constitution), DEX (dexterity), INT (intelligence), WIS (wisdom), and CHA (charisma); in the boardgame each of them is generated by rolling a six-sided die three times and adding up the rolls. Write a function randomStat( ) that randomly generates and returns such a number by adding together three random numbers between 1 and 6. You will then call this function six times in your main( ) to get the six stats — you don't need to write six versions of the function (that would defeat the purpose).
In your main( ), then, you will call the functions you wrote according to the above to display the character's name, class, and six statistics. The randomName( ), randomClass( ), randomStat( ) functions should not print anything out; they should merely return their results and let main( ) do the printing.
(Hint: remember random.choice( ) to choose a random element of a list and random.randint( ) to choose a random integer.)
Explanation / Answer
The python code for given question is
import random
def randomName():
first = ['Myrtis ','Liz ', 'Delmar ', 'Regina ' , 'Loyd ' , 'Lavonia ', 'Ed ', 'Lelah ', 'Shaneka ', 'Penny ']
second = ['Smith', 'Johnson', 'Williams', 'Jones', 'Brown', 'Davis', 'Miller', 'Wilson', 'Moore', 'Taylor']
return random.choice(first)+random.choice(second)
def randomClass():
classocu = ['chef', 'rancher', 'scientist', 'teacher', 'taxi driver', 'janitor', 'medical doctor', 'carpenter', 'blacksmith', 'florist', 'engineer', 'farmer']
return random.choice(classocu)
def ability(x):
stat = ['STR (strength)', 'CON (constitution)', 'DEX (dexterity)', 'INT (intelligence)', 'WIS (wisdom)', 'CHA (charisma)']
return stat[x]
def randomStat():
return random.randint(0,6)+random.randint(0,6)+random.randint(0,6)
print(randomName())
print(randomClass())
for x in range(0,6):
print(ability(x))
print(randomStat())
print()
The above code generate six players stats consists of values from randomName,randomClass and randomStat.
I hope you got the answer.
Thank you:)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.