Tell us what’s happening:
My test 44 is failing. I am unable to understand why? Can someone point to the error that I am making?
The issue relates to the str(self) method
Your code so far
class GameCharacter:
def __init__(self, name):
self._name=name
self._health=100
self._mana=50
self._level=1
@property
def name(self):
return self._name
@property
def health(self):
return self._health
@health.setter
def health(self, new_health):
if new_health<0:
self._health=0
elif new_health>100:
self._health=100
else:
self._health=new_health
@property
def mana(self):
return self._mana
@mana.setter
def mana(self,new_mana):
if new_mana<0:
self._mana=0
elif new_mana>50:
self._mana=50
else:
self._mana=new_mana
@property
def level(self):
return self._level
def level_up(self):
self._level +=1
self.health=100
self.mana=50
print(f"{self.name} leveled up to {self._level}!")
def __str__(self):
return f" Name: {self.name}\n Level: {self.level}\n Health: {self.health}\n Mana: {self.mana}"
hero=GameCharacter("Kratos")
print(hero)
hero.health -= 30
hero.mana -=10
print(hero)
hero.level_up()
print(hero)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Challenge Information:
Build a Game Character Stats Tracker - Build a Game Character Stats Tracker
Here are some troubleshooting steps you can follow. Focus on one test at a time:
- Are there any errors or messages in the console?
- What is the requirement of the first failing test?
- Check the related User Story and ensure it’s followed precisely.
- What line of code implements this?
- What is the result of the code and does it match the requirement? (Write the value of a variable to the console at that point in the code if needed.)
If this does not help you solve the problem, please reply with answers to these questions.
It looks like the example output is a bit mis-leading in that every line starts with a space.
Each line should not start with a space.
The main issue: Your string starts with a space and multi-line formatting may not match the expected output.
Notice:
- There is a leading space before every line.
- It starts with a newline-free but space-prefixed line.
- Many unit tests expect exact string match, so even one extra space or newline will cause a test failure.
Because unit tests compare strings exactly, and yours contains:
- A leading space " Name: …"
- A space before " Level:“, " Health:”, " Mana:"
- Even one extra character = test failure.
Here is correct code:
removed by mod
Please try it and let me know the result you get.
I’ll update my answer based on your response.
This question has already been answered. Please don’t respond to threads if you don’t have anything to add.
Please do not post LLM answers here.
Do not share solution code.
It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge. How to Help Someone with Their Code Using the Socratic Method
We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.
To answer your questions:
1.Yes there is an error in console: Screenshot attached
2. & 3. Your __str__ method should return a string with the character’s stats using the provided format.
“
Name: Kratos
Level: 1
Health: 100
Mana: 50
“
I have implemented without spaces at the begining of each line, then with single space there and lastly with a complete tab before each line.
4. ‘‘‘def _str_(self) → str:
return f"Name: {self.name}\\nLevel: {self.level}\\nHealth: {self.health}\\nMana: {self.mana}" ‘‘‘
-
Seriously, I have pasted my complete course and have been asking for help for about two days now.
Check my original post and help me rectify the issue
I have already tried with or without any space
I have strip, lstrip and rstrip. Additionally trailing spaces, leading spaces, no spaces, trailing tabs, leading tabs.
Now I am running out of options and getting frustrated without any legitimate help.
I have already posted my code. Referenced my str method, shared the problem statement and still nothing in 2 days
I have got the error. The issue got resolved after i changed my browser. The code is not working on chrome. However, it ran fine on Microsoft Edge
1 Like