Build an RPG Character - Build an RPG Character

Tell us what’s happening:

Been stuck on this all day. All tests pass, except for numbers 10, 12, and 14. At this point, anytime I change something, more tests fail. I feel like I’ve missed something in the information we were given, but I also thought I had done everything correctly. Could I get some help in figuring out why these three tests aren’t passing?

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')
    elif name == '':
        return('The character should have a name')
    elif len(name) > 10:
        return('The character name is too long')
    elif ' ' in name:
        return('The character name should not contain spaces')

    if not isinstance((strength) and (intelligence) and (charisma), int):
        return('All stats should be integers')
    elif (strength and intelligence and charisma) < 1:
        return('All stats should be no less than 1')
    elif (strength and intelligence and charisma) > 4:
        return('All stats should be no more than 4')
    elif (strength + intelligence + charisma) != 7:
        return('The character should start with 7 points')
    else:
        return(f'{name}\nSTR {full_dot*strength}{empty_dot*(10-strength)}\nINT {full_dot*intelligence}{empty_dot*(10-intelligence)}\nCHA {full_dot*charisma}{empty_dot*(10-charisma)}')

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/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36

Challenge Information:

Build an RPG Character - Build an RPG Character

Welcome to the forum @AideenMc ,

This is not doing what you think it is. EDITED: Please take a look at how to use isinstance() at the end of this theory lecture:
Understanding Variables and Data Types - What Are Common Data Types in Python and How Do You Get the Type of a Variable? | Learn | freeCodeCamp.org

Test your code with different values.

For instance, does this test fail where you think it should? print(create_character('ren', 1, 5, 1))

Happy coding!

I’ve tried it multiple different ways:

if (strength, intelligence, charisma) != int

if not isinstance((strength, intelligence, charisma), int

Do I need to add a new variable that combines those three? The only code that is working in that section is 16 and 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.

This is because I did elif (strength + intelligence + charisma) != 7:
return(‘The character should start with 7 points’)

So I think I’m just stuck on how to correctly format the other ones.

Apologies. I’ve edited my previous post. The first condition for strength is also not working because you are not using isinstance() correctly.

It also may help you to review this theory lecture:
Booleans and Conditionals - What Are Truthy and Falsy Values, and How Do Boolean Operators and Short-Circuiting Work? | Learn | freeCodeCamp.org

It sounds like a small edge case or requirement might be missing, especially if changing one thing breaks other tests. Double-check the problem instructions and test cases for boundary conditions (like empty input, limits, or formatting). Also try printing/debugging the failing cases (10, 12, 14) to see exactly where your output differs.

I’m still not understanding. I’ve gone back and read that section of the theory multiple times, and every different code I try doesn’t seem to work. Most recent attempt:

///
if not isinstance(strength, int):
    if not isinstance(intelligence, int):
        if not isinstance(charisma, int):
            return('All stats should be integers')

There’s no examples of isinstance() being used with multiple variables at a time, so I assume its not possible. I think I’ll try putting the (strength, intelligence, charisma) into a dictionary or list? I’ve read other people’s forum posts on this question and I don’t understand half of them, with some using for stat in stats.values(): or for stat in stats:, but I haven’t seen either of those ways used in the theory, and I’m not sure if those are the correct ways to do it, so Im trying to go off what I know, but I’m confusing myself.

I’ve tried:

stat_variables = {'STR': strength, 'INT': intelligence, 'CHA': charisma}
    if stat_variables != int: 
        return(‘All stats should be integers’)
    elif stat_variables < 1:
        return(‘All stats should be no less than 1’)
    elif stat_variables > 4:
        return(‘All stats should be no more than 4’)
    elif (strength + intelligence + charisma) != 7:
        return(‘The character should start with 7 points’)

And also tried changing

if stat_variables != int:

to

strength != int and intelligence != int and charisma != int

Either I get test 10 or 11 passing, but never both.

Smart not to use code you haven’t met yet! Half the users doing that sort of code aren’t using it correctly anyway…probably just copying someone else’s stuff without understanding it! :roll_eyes:

You are correct. You can’t use isinstance() to check multiple variables at one time. You can only use it to check multiple types at one time: e.g. isinstance(price, (int,float)), but that’s not relevant to this challenge.

What you can do though is construct your if statement so it uses multiple expressions, which is discussed in the second theory lecture link provided.

You could say:

if (expression1) or (expression2) or (expression3):
    do this

which means if any one of the expressions evaluates to False, do this.

Can you now construct an if statement using the and operator and the expressions you tried here?

Ok I’m getting closer!

I’ve changed it to:

stat_variables = {'STR': strength, 'INT': intelligence, 'CHA': charisma}

    if not(strength, int) and (intelligence, int) and (charisma, int):
        return ('All stats should be integers')

    elif (strength <1) or (intelligence <1) or (charisma < 1):
        return('All stats should be no less than 1')

    elif (strength >4) or (intelligence >4) or (charisma > 4):
        return('All stats should be no more than 4')

    elif (strength + intelligence + charisma) != 7:
        return('The character should start with 7 points')

    else:
        print(f'{name}\nSTR {full_dot*strength}{empty_dot*(10-strength)}\nINT {full_dot*intelligence}{empty_dot*(10-intelligence)}\nCHA {full_dot*charisma}{empty_dot*(10-charisma)}')

Tests 12 and 14 are now passing, but 10, 18 and 19 are not. If I change:

    if not(strength, int) and (intelligence, int) and (charisma, int):
        return ('All stats should be integers')

to:

    if (strength, int) and (intelligence, int) and (charisma, int):
        return not('All stats should be integers')

10, 12, 13, 16, 18 and 19 are incorrect, so if not(strength, int) and… seems to be getting close? I don’t really understand how changing that causes three other tests to not work, though. I guess because they’re within the same code block?

I also tried:

if not(strength, int) and not(intelligence, int) and not(charisma, int):
        return ('All stats should be integers')

Which doesn’t change anything.

Then I tried:

if (strength != int) and (intelligence != int) and (charisma != int):
    return ('All stats should be integers')

Which did pass test 10, but caused 11, 12, 14, 16, 18 and 19 to not pass :confused:

I also tried:

if (strength, int) and (intelligence, int) and (charisma, int):
    pass
else:
    return ('All stats should be integers')

I think I’ve been stuck on this for so long that I’ve melted my brain, but I’m getting there (I hope lol). Thank you for helping out by the way.

I need help figuring out 10, 18 and 19 now. I haven’t figured out my initial problem of test 10, but at least tests 12 and 14 are passing now? I don’t know where to go from here.

You are not using isinstance(). Why did you abandon your expressions below? Remember, each expression between the and operator is evaluated separately. These expressions are correct! You just need to join them with the and operator.

One other thing: you should be testing your code as you go. What happens when you print(not(strength, int))?

Don’t code blindly. Test your code!

Ok so my whole code is now:

full_dot = '●'
empty_dot = '○'

def create_character(name, strength, intelligence, charisma):
    if not isinstance(name, str): 
        return('The character name should be a string')

    elif name == '':
        return('The character should have a name')

    elif len(name) > 10:
        return('The character name is too long')

    elif ' ' in name:
        return('The character name should not contain spaces')

stat_variables = {'STR': strength, 'INT': intelligence, 'CHA': charisma}
    if not isinstance(strength, int) and not isinstance(intelligence, int) and not isinstance(charisma, int):
        return ('All stats should be integers')

    elif (strength <1) or (intelligence <1) or (charisma < 1):
        return('All stats should be no less than 1')

    elif (strength >4) or (intelligence >4) or (charisma > 4):
        return('All stats should be no more than 4')

    elif (strength + intelligence + charisma) != 7:
        return('The character should start with 7 points')

    else:
        print(f'{name}\nSTR {full_dot*strength}{empty_dot*(10-strength)}\nINT {full_dot*intelligence}{empty_dot*(10-intelligence)}\nCHA {full_dot*charisma}{empty_dot*(10-charisma)}')

create_character('ren', 4, 2, 1)
print(not(strength, int))

Codes 10, 18 and 19 not passing, and:

print(not(strength, int))

Is showing:

Traceback (most recent call last):
File “main.py”, line 31, in
NameError: name ‘strength’ is not defined

I don’t understand why strength is not defined? In other labs and workshops, I didn’t think the arguments needed to be defined.

I’m seeing this error in the console:

Traceback (most recent call last):
  File "main.py", line 18
    if not isinstance(strength, int) and not isinstance(intelligence, int) and not isinstance(charisma, int):
IndentationError: unexpected indent

You need to test inside your function!

That’s the first indent level though? If it was one less, it wouldn’t be inside the function, right?

After typing that test inside the function, it returned False. That’s helpful to know.

Hello,

In your code you’re doing :

if not isinstance(strength, int) and not isinstance(intelligence, int) and not isinstance(charisma, int)

So you print ‘All stats should be integers’ only when none of the stats is an int, but you should be printing it when any of the stats is not an integer. It’s not the and operator you’re supposed to use in this case but the or.

Hope this helps.

Correct. I just edited one of my replies again. And since I’m so out of it this morning after having a wisdom tooth extracted, I’m bowing out for the day. :zany_face:

Ah ok nice, I thought so, but got stuck on trying to make the and operator work instead :rofl:

Everything is now passing, other than 18 and 19. I’ll try a couple different things, but if either of you can spot where the issue is, please let me know!

Thank you both for your help :slight_smile:

Actually I just finished it. I won’t post the whole answer, but I had used ‘return’ after all of the ‘if’ statements, and ‘print’ on the very last one, to print the character output. The last two codes wouldn’t pass unless it was ‘return’ and not ‘print’, but when ‘return’ is used, there is no output showing in the terminal, though it passes? Any ideas why?

This was also an issue in the last lab; Build an Apply Discount Function. Where it also had all tests passing with the use of ‘return’ , but with nothing showing in the terminal, unless ‘print’ was used for the last one, but that meant it wouldn’t pass the tests.

Thank you so much for your help otherwise! Very much appreciated <3

The return means you’re returning the value, and the verification of the platform reads the value you returned, but that means you can’t see it.

Sometimes you’re asked to print the result so the verificator can read the console, in this case you’re also seeing the result.

But returning a string and printing a string is a different operation because when you’re only printing the string, the returned value of your function is None, which means it returns nothing.

It is very important to pay attention whether the platform asks you to print or return the string.

Good luck in your learning journey :slight_smile:

Ah okay that makes sense, thank you!