Hello,
I’m having and issue with test lines 17-19. I can’t seem to figure out the proper answer this exercise wants me to have. I have noticed that with coding / programming, there are multiple ways to achieve a goal but for the labs, this doesn’t seem to be the case.
I skipped a few steps to make a little bit more process but I am circling back around.
Your code so far
full_dot = '●'
empty_dot = '○'
#Charcater Info
character_name = 'Ace'
strength = 4
charisma = 2
intelligence = 1
STR = strength
CHA = charisma
INT = intelligence
stats = strength, charisma, intelligence
def create_character(character_name,strength,intelligence,charisma):
#Validates charcater name
#2-9
if not isinstance(character_name,str):
return 'The character name should be a string'
if character_name == "":
return 'The character should have a name'
if len(character_name) > 10:
return 'The character name is too long'
if ' ' in character_name:
return 'The character name should not contain spaces'
#Validates stats
#10-16
if not isinstance(strength, int):
return 'All stats should be integers'
if not isinstance(charisma, int):
return 'All stats should be integers'
if not isinstance(intelligence, int):
return 'All stats should be integers'
#-------------------------------------------------------------
if strength < 1:
return 'All stats should be no less than 1'
if charisma < 1:
return 'All stats should be no less than 1'
if intelligence < 1:
return 'All stats should be no less than 1'
#-------------------------------------------------------------
if strength > 4:
return 'All stats should be no more than 4'
if charisma > 4:
return 'All stats should be no more than 4'
if intelligence > 4:
return 'All stats should be no more than 4'
#-------------------------------------------------------------
if not isinstance((create_character, stats), int) == 7:
return 'The character should start with 7 points'
else:
pass
#Test with print() instead of return below:
print(character_name)
print('STR','●'*(strength) + '○'*(10-strength))
print('INT','●'*(intelligence) + '○'*(10-intelligence))
print('CHA','●'*(charisma) + '○'*(10-charisma))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36
Please review the followiing theory lecture about how to use isinstance(). You can check one variable against one or more types, but you cannot check multiple variables against a type. What does this method return? Does it return an integer?
Well, I cannot use this to find a solution because I can only use (2) arguments in a isinstance() function. When I try to use the:
create_character(character_name,strength,intelligence,charisma) method, it says It called for 2 arguements and got 4.
So then, I tried using “stats” to combine the 3 together and it’s still not working. I understand I’m trying to compare a string (stats) to a integer but I struggling to find the alternative…
create_character is the name of the function you are writing code in and create_character(character_name,strength,intelligence,charisma) is that function’s definition.
When you call the function like this: create_character('Flash',1,3,3), can you say what the value of strength will be? intelligence? charisma? These are the character’s stats. How do you get the total value of those stats?
But when I trying to verify all of that at the same time is where I think I’m getting lost. I know i can use the isinstance() function but that is just validating the boolean value… But I think I got test 17 fixed.
Please post your updated code formatted as follows:
There are two ways you can format your code to make it easier to read and test:
After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)
Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.
To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:
full_dot = '●'
empty_dot = '○'
#Charcater Info
character_name = 'Ace'
strength = 4
charisma = 2
intelligence = 1
STR = strength
CHA = charisma
INT = intelligence
stats = strength + charisma + intelligence
def create_character(character_name,strength, charisma, intelligence):
#Validates charcater name
#2-9
if not isinstance(character_name,str):
return 'The character name should be a string'
if character_name == "":
return 'The character should have a name'
if len(character_name) > 10:
return 'The character name is too long'
if ' ' in character_name:
return 'The character name should not contain spaces'
#Validates stats
#10-16
if not isinstance(strength, int):
return 'All stats should be integers'
if not isinstance(charisma, int):
return 'All stats should be integers'
if not isinstance(intelligence, int):
return 'All stats should be integers'
#-------------------------------------------------------------
if strength < 1:
return 'All stats should be no less than 1'
if charisma < 1:
return 'All stats should be no less than 1'
if intelligence < 1:
return 'All stats should be no less than 1'
#-------------------------------------------------------------
if strength > 4:
return 'All stats should be no more than 4'
if charisma > 4:
return 'All stats should be no more than 4'
if intelligence > 4:
return 'All stats should be no more than 4'
#-------------------------------------------------------------
if strength + intelligence + charisma != 7:
return 'The character should start with 7 points'
else:
pass
#Test with print() instead of return below:
print(character_name)
print('STR','●'*(strength) + '○'*(10-strength))
print('INT','●'*(intelligence) + '○'*(10-intelligence))
print('CHA','●'*(charisma) + '○'*(10-charisma))
# Ace
# STR ●●●●○○○○○○
# INT ●○○○○○○○○○
# CHA ●●○○○○○○○○
I think my code is really long and a bit redundant. Once I get the code completed, would you be willing to help me understand how to make this more concise and polished?
full_dot = '●'
empty_dot = '○'
#Charcater Info
character_name = 'Ace'
strength = 4
charisma = 2
intelligence = 1
STR = strength
CHA = charisma
INT = intelligence
stats = strength + charisma + intelligence
def create_character(character_name,strength, charisma, intelligence):
#Validates charcater name
#2-9
if not isinstance(character_name,str):
return 'The character name should be a string'
if character_name == "":
return 'The character should have a name'
if len(character_name) > 10:
return 'The character name is too long'
if ' ' in character_name:
return 'The character name should not contain spaces'
#Validates stats
#10-16
if not isinstance(strength, int):
return 'All stats should be integers'
if not isinstance(charisma, int):
return 'All stats should be integers'
if not isinstance(intelligence, int):
return 'All stats should be integers'
#-------------------------------------------------------------
if strength < 1:
return 'All stats should be no less than 1'
if charisma < 1:
return 'All stats should be no less than 1'
if intelligence < 1:
return 'All stats should be no less than 1'
#-------------------------------------------------------------
if strength > 4:
return 'All stats should be no more than 4'
if charisma > 4:
return 'All stats should be no more than 4'
if intelligence > 4:
return 'All stats should be no more than 4'
#-------------------------------------------------------------
if strength + intelligence + charisma != 7:
return 'The character should start with 7 points'
else:
return 'ren\nSTR ●●●●○○○○○○\nINT ●●○○○○○○○○\nCHA ●○○○○○○○○○'
#Test with print() instead of return below:
Why do I use the provided string (ren\nSTR ●●●●○○○○○○\nINT ●●○○○○○○○○\nCHA ●○○○○○○○○○) instead of having a “string” generated by the logic that is within the program?
It looks like you have hard-coded conditionals or variables that check for specific expected values. That is not solving this problem in the general case. Imagine if you were given different input values. Would your code be able to solve those problems?
To find out more about what hard-coding is or about why it is not suitable for solving coding questions, please read this post: Hard-coding For Beginners
Let us know if you have a question about how to make your code more flexible.