Build an RPG Character

What’s happening:

I’ve looked at many posts and haven’t found an answer. The terminal says #7, #11 and #12 are not passing.
I’ve been stuck on this a few days, tried different code from other posts and still it’s not working, despite seemingly working on desktop python.
The console shows the expected outcome but with “None” at the end, and I don’t know why that’s being returned.
thx!

Code so far

full_dot = '●'
empty_dot = '○'
def create_character(character_name, strength, intelligence, charisma):
    if not isinstance(character_name, str):
        return "The character name should be a string"
    elif character_name == "":
        return "The character should have a name"
    elif len(character_name) > 10:
        return "The character name is too long"
    elif character_name.count(" ") > 0:
        return "The character name should not contain spaces"
    stats = {strength, intelligence, charisma}
    for stat in stats:
        if not isinstance(stat, int):
            return "All stats should be integers"
        elif stat < 1:
            return "All stats should be no less than 1"
        elif stat > 4:
            return "All stats should be no more than 4"
    stats_sum = strength + intelligence + charisma
    if stats_sum != 7:
        return "The character should start with 7 points"
    def dots(value):
        return value * full_dot + (10 - value) * empty_dot
    print(f"{character_name} \n STR {dots(strength)} \n INT {dots(intelligence)} \n CHA {dots(charisma)}")
print(create_character("ren", 4, 2, 1))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0

Challenge Information:

Build an RPG Character

1 Like

try create_character('friend', {a: 3}, 2, 1)

the console returns NameError: name ‘a’ is not defined.
I just copied what was written, I hope that’s what I was supposed to do.

Shouldn’t it say All stats should be integers ? (if you’ve printed that function call to test it)

If one or more stats are not integers, the function should return All stats should be integers.

that would make sense to me, since that’s how the code is written, but it’s not the result.

I figured out why it was returning “none” at the end of the solution, I was writing print(create_character(‘ren’, 4, 2, 1)) when create_character already has a print() function, woops.

still don’t know why this part doesn’t work though:

stats = {strength, intelligence, charisma}

for stat in stats:

    if not isinstance(stat, int):

        return "All stats should be integers"

from my understanding, the line for stat in stats: runs each component of {stats} through the if not isinstance(stat, int): which evaluates whether each stat is an integer or not, and if it finds that any stat is not an integer, it returns “All stats should be integers”.

However, that doesn’t seem to be working in the tests, so I’m just confused. Am I misunderstanding how the “for … in … :” works?

Ok try this instead:

print(create_character("ren", {"a": 3}, 2, 1))

The previous test was throwing an error directly

 File "main.py", line 33, in <module>
NameError: name 'a' is not defined

Here’s what the terminal returns when I enter that :

Traceback (most recent call last)
 File "main.py", line 26 in <module>
 File "main.py", line 12 in create_character
Type error: unhashable type: 'dict'

Line 12 is this:

stats = {strength, intelligence, charisma}

I hope it’s representing a set, since I used the curly brackets. When I tried it with ( ) or [ ], the print didn’t work at all, so I think { } is the correct option in this case.
Since it represents a set, I hope the line

for stat in stats:

is calling on the set, but I’m not entirely sure if that’s how it’s supposed to work.

Line 26 is where I pasted

create_character("ren", {"a":3}, 2, 1)

I recognize that {“a”:3} is the format for dictionary, which I presume is why the terminal returns:

Type error: unhashable type: 'dict'

If I understand correctly, unhashable means it cannot have a hard-coded value? and in this case we’re giving “a” the value of 3?

Are my assumptions correct?
is how I think it’s supposed to work == how it actually works?

This is great work, you’ve narrowed down the problem to a line of code.

stats = {strength, intelligence, charisma}

Nothing wrong with this, but when there is that unexpected input, it’s causing an error. Try searching for the error to learn more about it

https://duckduckgo.com/?q=python+set+Type+error%253A+unhashable+type%253A+‘dict’

You tried a tuple and a list? What happened? Was there an error?

I’ll start by answering the question: The terminal comes back completely empty when i used ( ) and [ ]. No errors, just blank space.

I looked into the TypeError and how to “fix” it. It seems like I need to transform the set into a tuple, since I still need the order of the set components.

This is the code I’m entering to transform the set into a tuple; stats = tuple({strength:0, intelligence:1, charisma:2}.items())

instead of stats = {strength, intelligence, charisma}

I wasn’t sure if I requiredthe :0, :1, :2, so I removed them from the tuple. This lead to the terminal returning AttributeError: ‘set’ object has no attribute ‘items’ which leads me to believe I should keep these numbers in.

Since the second, third and fourth arguments have to be integers;
and a tuple forces the values to be integers;
wouldn’t transforming the set into a tuple force the values to be integers?
Shouldn’t this make any non-numerical inputs invalid?

Would that even trigger the “All stats should be integers” if one of the values is {“a”:3}, since it’s a dictionary key and cannot be processed in a tuple?

not really, it means that the thing you are putting in the set needs to have an hash method, which dictionaries and lists do not have, so you can’t use a set for this

also if a set forces things to be integers, how are you returning the requested string if they are not integers?

What data were you using to test?

Were you printing the result?

What’s your goal with this line?

stats = {strength, intelligence, charisma}

Why did you choose to gather these variables into a set specifically?

I would revisit this and test it a bit more. “No errors” is a step forward from where you are now, right?

Here’s the code I have now:

full_dot = '●'
empty_dot = '○'
def create_character(character_name, strength, intelligence, charisma):
    if not isinstance(character_name, str):
        return "The character name should be a string"
    elif character_name == "":
        return "The character should have a name"
    elif len(character_name) > 10:
        return "The character name is too long"
    elif character_name.count(" ") > 0:
        return "The character name should not contain spaces"
    stats = [strength, intelligence, charisma]
    for stat in stats:
        if not isinstance(stat, int):
            return "All stats should be integers"
        elif stat < 1:
            return "All stats should be no less than 1"
        elif stat > 4:
            return "All stats should be no more than 4"
    stats_sum = strength + intelligence + charisma
    if stats_sum != 7:
        return "The character should start with 7 points"
    def dots(value):
        return value * full_dot + (10 - value) * empty_dot
    print(f"{character_name}\nSTR {dots(strength)}\nINT {dots(intelligence)}\nCHA {dots(charisma)}")

The code now passes test 7;
It is now a problem of tests 11 and 12.

I’m still confused as to why we brought in the whole {‘a’:3} argument, even now it doesn’t work with the code as expected, despite passing test #7. (I’m expecting the code to return “All stats should be integers”, but it’s returning nothing at all)

Additionally, when I use arguments that should trigger any of the if statements, the terminal doesn’t return any text, it’s just empty. I have no idea why that stopped working. Weirdly, the code still passes every test up until #11.

I’ve removed the spaces around \n in print(f"{character_name}\nSTR {dots(strength)}\nINT {dots(intelligence)}\nCHA {dots(charisma)}")

but it still doesn’t pass the #11 test. The print is exactly as expected for create_character(‘ren’, 4, 2, 1), so I don’t know what’s gone wrong for the test to return negative.

a print is not the same as a return, so keep in mind that in this case your function is not returning anything

Are you calling your function and printing the result to test it? Because I don’t see that in your code.

Yes, I’ve tested it with multiple variations as well.

If I change the values to create_character(‘ren’, 5, 1, 1) the console is empty instead of returning “All stats should be no more than 4”, which is why I think there’s a problem.

I’m having a hard time identifying the problem, since it works as intended on the desktop python:

how are you testing your code? are you using a print to call the function in the editor?

can you share your updated code?

You need to use print to see what your function is returning

print(create_character('ren', 5, 1, 1))
1 Like

since there is already the print function within the create_character function, using print before it just returns the value “None” after ren\nSTR ●●●●○○○○○○\nINT ●●○○○○○○○○\nCHA ●○○○○○○○○○
If I remove the print function inside of create_character and instead use it as suggested print(create_character('ren', 5, 1, 1)), the terminal is empty.

Here is my current code:

full_dot = '●'

empty_dot = '○'

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

    if not isinstance(character_name, str):

        return "The character name should be a string"

    elif character_name == "":

        return "The character should have a name"

    elif len(character_name) > 10:

        return "The character name is too long"

    elif character_name.count(" ") > 0:

        return "The character name should not contain spaces"

    stats = [strength, intelligence, charisma]

    for x in stats:

        if not isinstance(x, int):

            return "All stats should be integers"

        elif x < 1:

            return "All stats should be no less than 1"

        elif x > 4:

            return "All stats should be no more than 4"

    stats_sum = strength + intelligence + charisma

    if stats_sum != 7:

        return "The character should start with 7 points"

    def dots(value):

        return value * full_dot + (10 - value) * empty_dot

    return print(f"{character_name}\nSTR {dots(strength)}\nINT {dots(intelligence)}\nCHA {dots(charisma)}")

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