Why does my integer validation fail in “Build an RPG Character” even when I use isinstance?

I’m doing the Build an RPG Character Python lab and I’m stuck on the tests related to validating that strength/intelligence/charisma are integers. My function looks fine when I run it, but the tests say it’s not handling non-integer stats correctly. I suspect my isinstance check is wrong, but I can’t understand why ?

What I expected:
If any stat is not an integer (example: "4" as a string), it should return:
All stats should be integers*

*What I’m getting:
Some invalid inputs still pass my integer check.

My code so far:–

full_dot = “●”
empty_dot = “○”

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

Name validation

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”

# Stat validation (I think this is wrong)
if not isinstance((strength) and (intelligence) and (charisma), int):
return “All stats should be integers”

Other checks (simplified here)

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”

Output formatting

return (
f"{name}\n"
f"STR {full_dot*strength}{empty_dot*(10-strength)}\n"
f"INT {full_dot*intelligence}{empty_dot*(10-intelligence)}\n"
f"CHA {full_dot*charisma}{empty_dot*(10-charisma)}"
)

print(create_character(“ren”, “4”, 2, 1))
print(create_character(“ren”, 4, 2, 1))

My question:
Why does isinstance((strength) and (intelligence) and (charisma), int) behave unexpectedly here, and what is the cleanest Pythonic way to validate all 3 stats are integers without writing 3 separate isinstance() checks?

1 Like
if not isinstance((strength) and (intelligence) and (charisma), int):

What happens here is that the (strength) and (intelligence) and (charisma) is first evaluated to bool data type. Result of that evaluation is then passed to isinstance. However, bool type in Python is subclass of int, so such isinstance check will always be True.

Leaving aside what might be the cleanest Pythonic way, generally loops can be used to reduce duplication when something repetitive is needed. Although from what I can see, they weren’t introduced yet.

Welcome to the forum @dhimankumar ,

You are not using isinstance() correctly. Check at the end of this theory lecture for the correct syntax and remember that each expression between the operators is evaluated separately.

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

Happy coding!

The proper formatting for using multiple isinstance() calls in one if statement is as follows:

def my_function(name, level)
if not (isinstance(name, str) and isinstance(level, str)):
    return true
else:
    return false

Can you format your code to match this template and see if you get a different result?

For future reference if you press the button highlighted in blue in this screenshot:

then click on “create a help post on the forum”:

You will get a properly formatted version of your code with all the details we need to help you easily and quickly debug your issue and provide you with guidance on how to proceed.

This is the way we prefer you to submit your help requests as any other way can make the process take longer and this formatting provides the most readable formatting and is easily copied and put inside of python environment in the event a community member is not able to solve it just by reading.

Happy coding! and I hope you are able to figure out your issue!

Don´t use ‘not’ - You should use boolean (True/False) in if- condition (isinstance(name, str) = False. Try his one:
removed by moderator

Welcome to the forum @soeren.bro !

There is nothing wrong with the syntax if not isinstance(strength, int) as opposed to if isinstance(strength,int) == False. Either are acceptable.

Also, please don’t provide solutions. Rather, it’s best to guide a user to discover their own errors by providing hints and/or debugging techniques. Thank you.

Happy coding!

Quite right. Thank for guidence.

To be exact, using not is preferred in Python. Only cases when, it matters which exactly truthy/falsy value variable has, would not use it.

Ie. when value can be either bool, or None, explicit comparison with True/False might be useful. However when explicitly comparing both booleans and None, they should be identity compared, with is/is not operator instead of equality comparison with ==/!=.

1 Like

This usually happens because the value you’re validating isn’t actually an int, even if it looks like one. For example, inputs from forms or input() are always strings, so isinstance(value, int) will return False.

Try converting the value first using int() inside a try/except block, and then validate it. Also watch out for cases like floats (10.0) or booleans, since bool is technically a subclass of int in Python and can give unexpected results.