Hi, can someone tell me if I made errors in my code ? I can’t get any test to pass even though the seems logical. but everytime I check it, it tells me that my code raised errors.
Your code so far
full_dot = '●'
empty_dot = '○'
def create_character(name, strength, intelligence, charsima):
if isinstance(name, str) == False:
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'
if not isinstance(strength, int) or not isinstance(intelligence, int) or not isinstance(charisma, int):
return 'All stats should be integers'
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 + charsima != 7:
return 'The character should start with 7 points'
else:
return (character_name)
return ('STR ' + strength*full_dot + (10-strength)*empty_dot)
return ('INT ' + intelligence*full_dot + (10-intelligence)*empty_dot)
return ('CHA ' + charisma * full_dot + (10-charisma)*empty_dot)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
You said that the code stops processing once it gets to a return, what did you imply ? The function doesn’t stop after one return function right ? Sorry if I’m bothering.
You are not bothering me. This is an important point to understand.
Look at your first if statement, please. If you call your function with a number for the name parameter (create_character(2,4,2,1)), then you would expect name not to validate, right? So, immediately, your code returns the message, “The character name should be a string” and it stops processing right there. It never “sees” any of the following if statements.
Ohhh okay thanks for the reply. But then how can my code continue to read the following lines if it stops after a return ? I don’t want the solution but this seems weird because that means that every function just stops after a return ? Does that mean I can’t use return ?
If there is a name and name is a string and name is not over 10 characters long and there are no spaces in name, your code will not go to the return statements.