I am very grateful that we have all this for free, but these assertion tests are a pain in the **s. I spent several hours to develop the code to meet the requirements and it outputs exactly the same as I see it in my Edge browser. Now, I need to change the code to meet the assertion test that probably not meet the requirements. I have no idea what to do with dev tools from the Edge. I never used them and never used js. It should not be necessary to learn that. I am here for Python not js… Can someone please help me with what is the wrong? I want to take the certification and having the test passed is a requirement.
class Category:
def __init__(self, category):
self.ledger = []
self.category = category
def __str__(self):
bal = self.get_balance()
my_str = self.category.center(30, "*") + "\n"
for t in self.ledger:
my_str += t['description'][:23].ljust(23)
s = str(t['amount']).split(".")
if len(s) == 1:
s = s[0] + ".00"
else:
s = s[0] + "." + str(s[1])
my_str += s.rjust(7) + "\n"
my_str += f"Total: {bal}"
return my_str
def deposit(self, amount, description = ""):
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self, amount, description = ""):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': description})
return True
else:
return False
def get_balance(self):
balance = 0
for t in self.ledger:
balance += t['amount']
return (balance)
def transfer(self, amount, category):
if self.check_funds(amount):
self.withdraw(amount, f'Transfer to {category.category}')
category.deposit(amount, f'Transfer from {self.category}')
return True
else:
return False
def check_funds(self, amount):
return False if self.get_balance() - amount < 0 else True
def create_spend_chart(categories):
cat_total = {}
cat_names = []
total=0
for cat in categories:
local=0
for _ in cat.ledger:
if _['amount'] < 0:
local += _['amount']
total += local
cat_total[cat.category] = local
cat_names.append(cat.category)
s = ""
for i in range(100,-10,-10):
s += f'{i}|'.rjust(4)
for key in categories:
partial = cat_total[key.category]*100/total
if i < partial:
s += " o"
else:
s += " "
s += "\n"
s = f'Percentage spent by category\n' + s
legend = " " + "".ljust(2*len(categories), "-") + '--'
max_l = 0
for i in cat_names:
max_l = max(max_l,len(i))
for i in range(0, max_l+1):
legend += "\n "
for c in cat_names:
if len(c) > i:
legend += " " + c[i]
else:
legend += " "
return s + legend
food = Category('Food')
food.deposit(1000, 'deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
clothing.deposit(1000, 'deposit')
clothing.withdraw(55.19, 'frake de merda')
food.transfer(50, clothing)
print(create_spend_chart({food,clothing}))
// running tests
19. The height of each bar on the create_spend_chart chart should be rounded down to the nearest 10.
20. Each line in create_spend_chart chart should have the same length. Bars for different categories should be separated by two spaces, with additional two spaces after the final bar.
21. create_spend_chart should correctly show horizontal line below the bars. Using three - characters for each category, and in total going two characters past the final bar.
23. create_spend_chart chart should have each category name written vertically below the bar. Each line should have the same length, each category should be separated by two spaces, with additional two spaces after the final category.
24. create_spend_chart should print a different chart representation. Check that all spacing is exact. Open your browser console with F12 for more details.
// tests completed
// console output
Percentage spent by category
100|
90|
80|
70|
60|
50| o
40| o o
30| o o
20| o o
10| o o
0| o o
------
F C
o l
o o
d t
h
i
n
g
Percentage spent by category
100|
90|
80|
70|
60|
50| o
40| o o
30| o o
20| o o
10| o o
0| o o
------
C F
l o
o o
t d
h
i
n
g
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
Boy do i have the perfect link for you. This should clear up your issue and help you pass this test. If this does help you i just request that you mark this post as the solution to this topic that you have created. Happy Coding!
P.S. storing the entire chart in one variable helped me pass some tests near the end. Example: print(my_chart_variable) where i had concatenated or joined everything together throughout my code working left to right, and then top to bottom.
Hei Drewlius, the description of the length of each line definitely made a positive impact and I could pass the 19 test. I changed the code and now it is printing 26 chars for the title and 17 for every other row. Just like your example. Still, I get errors on the test 20, 21, 23 and 24.
It is completely ridiculous that we have to spend time more than 2x the time used for learning python on fidging with what the assertion tests are actually testing. I cannot see them. If someone can help by posting the assertion tests, it would be a great help.
Percentage spent by category
100|
90|
80|
70| o
60| o
50| o
40| o
30| o
20| o
10| o o
0| o o o o
-------------
C F U H
l o n a
o o d r
t d e d
h r w
i w a
n a r
g r e
e
and this is the output with "+" instead of spaces
So your lines all need to be the same length for step 20 not including the title line. You should have empty spaces following the bar chart to equal the length of the Title.
for step 21 your bar chart should look like this I have included my line lengths for each line in this snippet. As for the rest of your steps it just appears that you maybe do not have the spacing configured quite properly. Test it with this string. replace enumerate(lines) with whatever variable you are storing your lines inside of in your code
for i, line in enumerate(lines):
print(f"Line {i}: length = {len(line)}")
Then add this to the bottom of your code to test it against the example I have provided below.
food = Category('Food')
clothing = Category('Clothing')
software = Category('Software')
hardware = Category('Hardware')
food.deposit(1000, 'initial deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
food.transfer(50, clothing)
clothing.deposit(100, 'initial deposit')
clothing.withdraw(16.00, 'pants')
clothing.withdraw(56.00, 'shirts')
hardware.deposit(1000, 'initial depsoit')
hardware.withdraw(350.56, 'new CPU')
hardware.withdraw(124.33, 'new Motherboard')
software.deposit(250.00, 'initial deposit')
software.withdraw(200, 'Windows 11')
print(food)
print(clothing)
print(software)
print(hardware)
#replace create_spend_chart with the variable you are using to store your chart output in.
print(create_spend_chart([food, clothing, software, hardware]))
Line 0: length = 28
Line 1: length = 17
Line 2: length = 17
Line 3: length = 17
Line 4: length = 17
Line 5: length = 17
Line 6: length = 17
Line 7: length = 17
Line 8: length = 17
Line 9: length = 17
Line 10: length = 17
Line 11: length = 17
Line 12: length = 17
Line 13: length = 17
Line 14: length = 17
Line 15: length = 17
Line 16: length = 17
Line 17: length = 17
Line 18: length = 17
Line 19: length = 17
Line 20: length = 17
Percentage spent by category
100|
90|
80|
70|
60|
50| o
40| o
30| o
20| o o
10| o o
0| o o o o
-------------
F C S H
o l o a
o o f r
d t t d
h w w
i a a
n r r
g e e
Also you should not have that extra line at the bottom of your chart. If that is what is actually appearing in your terminal that is incorrect.
Thank you! Your help was precious and helped me to troubleshoot the issues.
It would be straightfoward if they have used “.” instead of “ “for the visual example that they include in the project!
@ruim Im glad you were able to get it figured out. Could you mark my submission that helped you solve the question as solved. it is a little square box near the reply button i would greatly appreciate it!