Build an RPG Character - Build an RPG Character

Tell us what’s happening:

I spent my night trying to understand what I did wrong, haven’t found anything that could help me with my case on the forum, I think I spelt something wrong, or maybe I missed an indent or a quote.

Your code so far

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')
#about the name
#stats
    stats={'STR ':strength, 'INT ':intelligence, 'CHA ':charisma}
    for stat in stats.values():
        if type(stat) is not int:
            return 'All stats should be integers'

        if stat < 1:
            return('All stats should be no less than 1')
        
        if stat > 4:
            return('All stats should be no more than 4')
        
        if sum(stats.values()) != 7:
            return('The character should start with 7 points')
#stats
#formating related
    character_string=name
    for key in ['STR ', 'INT ', 'CHA ']:
        stat=stats[key]
        character_string += f'\n{key}{full_dot*stat}{empty_dot*(10-stat)}'
    return character_string
#formattng related
#function end
print(create_character('ren', 4, 2, 1))

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.4 Safari/605.1.15

Challenge Information:

Build an RPG Character - Build an RPG Character

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-rpg-character/67d83df6f82eda3868dd2a84.md at main · freeCodeCamp/freeCodeCamp · GitHub

You’re using a loop to check whether all stats are integers and to validate the other conditions, which is good.

However, there is an issue with the code above. You’re checking the sum of all stats inside the loop. Consider these examples:

print(create_character('ren', '4', 2, 1))

This works correctly because the second parameter is not an integer, so the function returns:

All stats should be integers

But with:

print(create_character('ren', 4, '2', 1))

you’ll get an error when calculating the sum because one of the stats is still a string. I suggest moving the sum check outside the loop, after you’ve confirmed that all stats are integers. This will prevent the error and ensure the validations are performed in the correct order.