curriculum/challenges/english/blocks/lab-rpg-character/67d83df6f82eda3868dd2a84.md
In this lab you will practice the basics of Python by building a small app that creates a character for an RPG adventure.
Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.
User Stories:
create_character.The character name should be a string.The character should have a name.The character name is too long.The character name should not contain spaces.All stats should be integers.All stats should be no less than 1.All stats should be no more than 4.The character should start with 7 points.STR, INT or CHA (in this order), then a space, and then a number of full dots (●) equal to the value of the stat, and a number of empty dots (○) to reach 10. Example: if the value of strength is 3 there must be 3 full dots followed by 7 empty dots. The dots are given in the editor.Here's the string that should be returned by create_character('ren', 4, 2, 1):
ren
STR ●●●●○○○○○○
INT ●●○○○○○○○○
CHA ●○○○○○○○○○
NOTE: while str and int are common abbreviations for the stats, remember that those are reserved keywords in Python and should not be used as variable names.
You should have a function named create_character.
({
test: () => runPython(
`
assert _Node(_code).has_function('create_character')
`
)
})
When create_character is called with a first argument that is not a string it should return The character name should be a string.
({
test: () => runPython(
`
wrong_types = [3, 5, {'a': 3}, 3.1, [1, 2], (1, 2), {1, 2}]
for wrong_type in wrong_types:
assert create_character(wrong_type, 1, 2, 4) == 'The character name should be a string'
`
)
})
When create_character is called with a first argument that is a string it should not return The character name should be a string.
({
test: () => runPython(
`assert create_character('ren', 1, 2, 4) != 'The character name should be a string'`
)
})
When create_character is called with a first argument that is an empty string, it should return The character should have a name.
({
test: () => runPython(
`
assert create_character('', 4, 2, 1) == 'The character should have a name'
`
)
})
When create_character is called with a first argument that is not an empty string, it should not return The character should have a name.
({
test: () => runPython(
`
assert create_character('ren', 4, 2, 1) != 'The character should have a name'
`
)
})
When create_character is called with a first argument that is longer than 10 characters it should return The character name is too long.
({
test: () => runPython(
`
for length in range(11, 27):
assert create_character('abcdefghijklmnopqrstuvwxyz'[:length], 4, 2, 1) == 'The character name is too long'
`
)
})
The create_character function should not say that the character is too long when it's not longer than 10 characters.
({
test: () => runPython(
`
for length in range(0, 11):
assert create_character('aaabbcccdd'[:length], 4, 2, 1) != 'The character name is too long'
`
)
})
When create_character is called with a first argument that contains a space it should return The character name should not contain spaces.
({
test: () => runPython(
`
assert create_character('cha cha', 4, 1, 2) == 'The character name should not contain spaces'
`
)
})
When create_character is called with a first argument that does not contain a space it should not return The character name should not contain spaces.
({
test: () => runPython(
`
assert create_character('chacha', 4, 2, 1) != 'The character name should not contain spaces'
`
)
})
When create_character is called with a second, third or fourth argument that is not an integer it should return All stats should be integers.
({
test: () => runPython(
`
wrong_types = ['3', '5', {'a': 3}, 3.1, [1, 2], (1, 2), {1, 2}, 'str', 'friend']
for wrong_type in wrong_types:
assert create_character('friend', wrong_type, 2, 1) == 'All stats should be integers'
assert create_character('friend', 2, wrong_type, 1) == 'All stats should be integers'
assert create_character('friend', 2, 1, wrong_type) == 'All stats should be integers'
`
)
})
When create_character is called with a second, third and fourth argument that are all integers it should not return All stats should be integers.
({
test: () => runPython(
`
assert create_character('friend', 4, 2, 1) != 'All stats should be integers'
`
)
})
When create_character is called with a second, third or fourth argument that is lower than 1 it should return All stats should be no less than 1.
({
test: () => runPython(
`
expected = 'All stats should be no less than 1'
assert create_character('ren', 0, 4, 3) == expected
assert create_character('ren', 4, 0, 3) == expected
assert create_character('ren', 4, 3, 0) == expected
assert create_character('ren', -1, 4, 4) == expected
assert create_character('ren', 4, -1, 4) == expected
assert create_character('ren', 4, 4, -1) == expected
`
)
})
When create_character is called with a second, third and fourth argument that are all no less than 1 it should not return All stats should be no less than 1.
({
test: () => runPython(
`
assert create_character('ren', 4, 2, 1) != 'All stats should be no less than 1'
`
)
})
When create_character is called with a second, third or fourth argument that is higher than 4 it should return All stats should be no more than 4.
({
test: () => runPython(
`
assert create_character('ren', 1, 5, 1) == 'All stats should be no more than 4'
assert create_character('ren', 5, 1, 1) == 'All stats should be no more than 4'
assert create_character('ren', 1, 1, 5) == 'All stats should be no more than 4'
`
)
})
When create_character is called with a second, third and fourth argument that are all no more than 4 it should not return All stats should be no more than 4.
({
test: () => runPython(
`
assert create_character('ren', 4, 2, 1) != 'All stats should be no more than 4'
`
)
})
When create_character is called with a second, third or fourth argument that do not sum to 7 it should return The character should start with 7 points.
({
test: () => runPython(
`
assert create_character('ren', 4, 4, 4) == 'The character should start with 7 points'
assert create_character('ren', 1, 1, 1) == 'The character should start with 7 points'
assert create_character('ren', 1, 2, 3) == 'The character should start with 7 points'
`
)
})
When create_character is called with a second, third and fourth argument that sum to 7 it should not return The character should start with 7 points.
({
test: () => runPython(
`
assert create_character('ren', 4, 2, 1) != 'The character should start with 7 points'
`
)
})
create_character('ren', 4, 2, 1) should return ren\nSTR ●●●●○○○○○○\nINT ●●○○○○○○○○\nCHA ●○○○○○○○○○.
({
test: () => runPython(
`
assert create_character('ren', 4, 2, 1) == f'ren\\nSTR ●●●●○○○○○○\\nINT ●●○○○○○○○○\\nCHA ●○○○○○○○○○'
`
)
})
When create_character is called with valid values it should output the character stats as required.
({
test: () => runPython(
`
assert create_character('Bob', 1, 2, 4) == f'Bob\\nSTR ●○○○○○○○○○\\nINT ●●○○○○○○○○\\nCHA ●●●●○○○○○○'
assert create_character('aaabbbcccd', 4, 2, 1) == f'aaabbbcccd\\nSTR ●●●●○○○○○○\\nINT ●●○○○○○○○○\\nCHA ●○○○○○○○○○'
`
)
})
full_dot = '●'
empty_dot = '○'
full_dot = '●'
empty_dot = '○'
def create_character(character_name, strength, intelligence, charisma):
if type(character_name) != str:
return 'The character name should be a string'
if len(character_name) == 0:
return 'The character should have a name'
if len(character_name) > 10:
return 'The character name is too long'
if ' ' in character_name:
return 'The character name should not contain spaces'
if type(strength) != int or type(intelligence) != int or type(charisma) != int:
return 'All stats should be integers'
if strength < 1 or intelligence < 1 or charisma < 1:
return 'All stats should be no less than 1'
if strength > 4 or intelligence > 4 or charisma > 4:
return 'All stats should be no more than 4'
if strength + intelligence + charisma != 7:
return 'The character should start with 7 points'
character_sheet = f'{character_name}\n'
character_sheet += f'STR {full_dot * strength}{empty_dot * (10 - strength)}\n'
character_sheet += f'INT {full_dot * intelligence}{empty_dot * (10 - intelligence)}\n'
character_sheet += f'CHA {full_dot * charisma}{empty_dot * (10 - charisma)}'
return character_sheet