mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-13 16:04:36 -04:00
feat: update Build an RPG Character lab (#64214)
Co-authored-by: Dario <105294544+Dario-DC@users.noreply.github.com>
This commit is contained in:
@@ -17,6 +17,7 @@ In this lab you will practice the basics of Python by building a small app that
|
||||
1. The function should accept, in order, a character name followed by three stats: strength, intelligence, and charisma.
|
||||
1. The character name should be validated:
|
||||
- If the character name is not a string, the function should return `The character name should be a string`.
|
||||
- If the character name is an empty string, the function should return `The character should have a name`.
|
||||
- If the character name is longer than 10 characters, the function should return `The character name is too long`.
|
||||
- If the character name contains spaces, the function should return `The character name should not contain spaces`.
|
||||
1. The stats should also be validated:
|
||||
@@ -28,7 +29,7 @@ In this lab you will practice the basics of Python by building a small app that
|
||||
- the first line should contain the character name
|
||||
- lines 2-4 should start with the stat abbreviation, `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 `create_character("ren", 4, 2, 1)` should return:
|
||||
Here's the string that should be returned by `create_character('ren', 4, 2, 1)`:
|
||||
|
||||
```md
|
||||
ren
|
||||
@@ -59,7 +60,7 @@ When `create_character` is called with a first argument that is not a string it
|
||||
({
|
||||
test: () => runPython(
|
||||
`
|
||||
wrong_types = [3, 5, {"a": 3}, 3.1, [1, 2], (1, 2), {1, 2}]
|
||||
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'
|
||||
|
||||
@@ -71,13 +72,39 @@ assert create_character(1, 1, 1, 1) == '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`.
|
||||
|
||||
```js
|
||||
({
|
||||
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 longer than 10 characters it should return `The character name is too long`.
|
||||
|
||||
```js
|
||||
({
|
||||
test: () => runPython(
|
||||
`
|
||||
assert create_character("aaabbbcccdd", 4, 2, 1) == 'The character name is too long'
|
||||
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.
|
||||
|
||||
```js
|
||||
({
|
||||
test: () => runPython(
|
||||
`
|
||||
for length in range(0, 11):
|
||||
assert create_character('aaabbcccdd'[:length], 4, 2, 1) != 'The character name is too long'
|
||||
`
|
||||
)
|
||||
})
|
||||
@@ -89,7 +116,7 @@ When `create_character` is called with a first argument that contains a space it
|
||||
({
|
||||
test: () => runPython(
|
||||
`
|
||||
assert create_character("cha cha", 4, 1, 2) == 'The character name should not contain spaces'
|
||||
assert create_character('cha cha', 4, 1, 2) == 'The character name should not contain spaces'
|
||||
`
|
||||
)
|
||||
})
|
||||
@@ -101,11 +128,11 @@ When `create_character` is called with a second, third or fourth argument that i
|
||||
({
|
||||
test: () => runPython(
|
||||
`
|
||||
wrong_types = ["3", "5", {"a": 3}, 3.1, [1, 2], (1, 2), {1, 2}, "str", "friend"]
|
||||
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'
|
||||
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'
|
||||
`
|
||||
)
|
||||
})
|
||||
@@ -117,14 +144,14 @@ When `create_character` is called with a second, third or fourth argument that i
|
||||
({
|
||||
test: () => runPython(
|
||||
`
|
||||
expected = "All stats should be no less than 1"
|
||||
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
|
||||
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
|
||||
`
|
||||
)
|
||||
})
|
||||
@@ -136,12 +163,12 @@ When `create_character` is called with a second, third or fourth argument that i
|
||||
({
|
||||
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'
|
||||
assert create_character("ren", 1, 1, 7) == 'All stats should be no more than 4'
|
||||
assert create_character("ren", 1, 7, 1) == 'All stats should be no more than 4'
|
||||
assert create_character("ren", 7, 1, 1) == 'All stats should be no more than 4'
|
||||
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'
|
||||
assert create_character('ren', 1, 1, 7) == 'All stats should be no more than 4'
|
||||
assert create_character('ren', 1, 7, 1) == 'All stats should be no more than 4'
|
||||
assert create_character('ren', 7, 1, 1) == 'All stats should be no more than 4'
|
||||
`
|
||||
)
|
||||
})
|
||||
@@ -153,21 +180,21 @@ When `create_character` is called with a second, third or fourth argument that d
|
||||
({
|
||||
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'
|
||||
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'
|
||||
`
|
||||
)
|
||||
})
|
||||
```
|
||||
|
||||
`create_character("ren", 4, 2, 1)` should return `ren\nSTR ●●●●○○○○○○\nINT ●●○○○○○○○○\nCHA ●○○○○○○○○○`.
|
||||
`create_character('ren', 4, 2, 1)` should return `ren\nSTR ●●●●○○○○○○\nINT ●●○○○○○○○○\nCHA ●○○○○○○○○○`.
|
||||
|
||||
```js
|
||||
({
|
||||
test: () => runPython(
|
||||
`
|
||||
assert create_character("ren", 4, 2, 1) == f'ren\\nSTR ●●●●○○○○○○\\nINT ●●○○○○○○○○\\nCHA ●○○○○○○○○○'
|
||||
assert create_character('ren', 4, 2, 1) == f'ren\\nSTR ●●●●○○○○○○\\nINT ●●○○○○○○○○\\nCHA ●○○○○○○○○○'
|
||||
`
|
||||
)
|
||||
})
|
||||
@@ -179,8 +206,8 @@ When `create_character` is called with valid values it should output the charact
|
||||
({
|
||||
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 ●○○○○○○○○○'
|
||||
assert create_character('Bob', 1, 2, 4) == f'Bob\\nSTR ●○○○○○○○○○\\nINT ●●○○○○○○○○\\nCHA ●●●●○○○○○○'
|
||||
assert create_character('aaabbbcccd', 4, 2, 1) == f'aaabbbcccd\\nSTR ●●●●○○○○○○\\nINT ●●○○○○○○○○\\nCHA ●○○○○○○○○○'
|
||||
`
|
||||
)
|
||||
})
|
||||
@@ -206,20 +233,22 @@ empty_dot = '○'
|
||||
|
||||
def create_character(character_name, strength, intelligence, charisma):
|
||||
if type(character_name) != str:
|
||||
return "The character name should be a string"
|
||||
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"
|
||||
return 'The character name is too long'
|
||||
if ' ' in character_name:
|
||||
return "The character name should not contain spaces"
|
||||
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"
|
||||
return 'All stats should be integers'
|
||||
if strength < 1 or intelligence < 1 or charisma < 1:
|
||||
return "All stats should be no less than 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"
|
||||
return 'All stats should be no more than 4'
|
||||
if strength + intelligence + charisma != 7:
|
||||
return "The character should start with 7 points"
|
||||
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'
|
||||
|
||||
Reference in New Issue
Block a user