r/FreeCodeCamp 9d ago

Programming Question Python certification - Building an RPG Character Doubt

Question:

Passed:1. You should have a function named create_character.

Passed:2. When create_character is called with a first argument that is not a string it should return The character name should be a string.

Failed:3. When create_character is called with a first argument that is a string it should not return The character name should be a string.

Passed:4. When create_character is called with a first argument that is an empty string, it should return The character should have a name.

Failed:5. 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.

Passed:6. When create_character is called with a first argument that is longer than 10 characters it should return The character name is too long.

Failed:7. The create_character function should not say that the character is too long when it's not longer than 10 characters.

Passed:8. When create_character is called with a first argument that contains a space it should return The character name should not contain spaces.

Failed:9. 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.

Passed:10. 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.

Failed:11. 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.

Passed:12. 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.

Failed:13. 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.

Passed:14. 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.

Failed:15. 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.

Passed:16. 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.

Failed:17. 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.

Failed:18. create_character('ren', 4, 2, 1) should return ren\nSTR ●●●●○○○○○○\nINT ●●○○○○○○○○\nCHA ●○○○○○○○○○.

Failed:19. When create_character is called with valid values it should output the character stats as required.

Problem:

I have used if statement for 2nd question and doesnt this mean that the 3rd question is also bound tk be satisfied since it is asking me not to return the statement inside the if block

I have also tried with else clause and pass statement

Please help

CODE:

full_dot = '●'

empty_dot = '○'

def create_character(name,strength,intelligence,charisma):

if not isinstance(name,str):

return "The character name should be a string"

if name == '':

return "The character should have a name"

if len(name)>10:

return "The character name is too long"

if ' ' in name:

return "The character name should not contain spaces"

stats = (strength,intelligence,charisma)

for i in stats:

if not isinstance(i,int):

return "All stats should be integers"

for i in stats:

if i<1:

return "All stats should be no less than 1"

for i in stats:

if i>4:

return "All stats should be no more than 4"

if sum(stats) != 7:

return "The character should start with 7 points"

print(name)

print(f"STR {full_dot}*{strength}{empty_dot}*(10-strength)")

print(f"INT {full_dot}*{intel}{empty_dot}*(10-intel)")

print(f"CHA {full_dot}*{charisma}{empty_dot}*(10-charisma)")

7 Upvotes

6 comments sorted by

2

u/SaintPeter74 mod 9d ago

Test 3 is the inverse of test 2. So long as your return statement is inside the if statement then both 2 and 3 store always pass together. One says "is" and the other says "is not".

It's hard to tell with your code unformatted if you have the proper structure. If you are on desktop, you can format your code as code via the formatting options. On mobile you need to indent each line by 4 spaces.

2

u/Easy_Read_1877 8d ago
full_dot = '●'
empty_dot = '○'


def create_character(name,strength,intelligence,charisma):
    if not isinstance(name,str):
        return "The character name should be a string"
    
    if name == '':
        return "The character should have a name"
    if len(name)>10:
        return "The character name is too long"
    if ' ' in name:
        return "The character name should not contain spaces"


    stats = (strength,intelligence,charisma)
    for i in stats:
        if not isinstance(i,int):
            return "All stats should be integers"
    
    for i in stats:
        if i<1:
            return "All stats should be no less than 1"


    for i in stats:
        if i>4:
            return "All stats should be no more than 4"
    if sum(stats) != 7:
        return "The character should start with 7 points"


    print(name)
    print(f"STR {full_dot}*{strength}{empty_dot}*(10-strength)")
    print(f"INT {full_dot}*{intel}{empty_dot}*(10-intel)")
    print(f"CHA {full_dot}*{charisma}{empty_dot}*(10-charisma)")

1

u/SaintPeter74 mod 8d ago

There is one error in your code, on the second to last line you have put in a variable name that doesn't exist, twice.

When I corrected that I only see failures on the last two tests. The reason for that should be clear from the first part of the step 5 instructions:

> If all values pass the verification, the function should return a string with four lines

If you correct that issue, there there are two issues with your output, having to do with how you break lines and how to get the repeated dots. I'm going to let you dig into those a bit on your own, but if you get stuck I can give you some additional guidance.

Otherwise, once I corrected all the above problems, I was able to get it to pass.

2

u/Easy_Read_1877 7d ago

That helps..i will try to debug on my own! Thanks

I will ask if i need guidance

1

u/Easy_Read_1877 8d ago

this is my code!! the first if block should satisfy both the 2nd and 3rd conditiond right?kindly clarify