In the Replit ‘budget project’ the tests in ‘test_module.py’ and the tests in ‘main.py’ don’t correspond. Is this normal? Because this is the reason my code keeps failing.
What do you mean they don’t correspond?
In main.py
is just an example, it’s not really a test.
Is the ledger shared between all categories, or categories using the same name (between different instances)?
@sanity identified the problem exactly. You should not have a global variable or class variable holding any data. All data must only be in instance variables.
I don’t really understand how am I supposed to test my code?
It would really help if you post a link to your code.
You test the code by clicking on the Run button.
class Category:
ledger = []
name = ''
withdrawals = 0
def __init__(self, name):
self.name = name
def deposit (self, amount, description = ''):
self.ledger.append({"amount": amount, "description": description})
def check_funds(self, amount):
fund = 0
for i in self.ledger:
fund += i["amount"]
if fund >= amount :
ans = True
else:
ans = False
return ans
def withdraw(self, withdraw, description =''):
fund = self.check_funds(withdraw)
if fund == True :
self.ledger.append({"amount": withdraw*(-1), "description": description})
self.withdrawals -= withdraw
ans = True
else:
ans = False
return ans
def get_balance(self):
balance = 0
for i in self.ledger:
balance += i["amount"]
return balance
def transfer(self, amount, destination):
if self.check_funds(amount) == True:
self.ledger.append({"amount": amount*(-1), "description": "Transfer to {}".format(destination.name)})
self.withdrawals -= amount
destination.ledger.append({"amount": amount, "description": "Transfer from {}".format(self.name)})
ans = True
else:
ans = False
return ans
def __str__(self):
amount_of_crosses = int(30 - len(self.name) /2)
end_string = "*"*amount_of_crosses + self.name + '*'*amount_of_crosses+'\n'
for i in self.ledger:
spaces = 7 - len(str(i['amount']))
try:
end_string += i['description'][:23] + ' '*spaces + str(round(float(i['amount']), 2)) + '\n'
except:
space_for_description = 23 - len(i['description'])
end_string += i['description'] +' '* space_for_description +' '*spaces + str(round(float(i['amount']), 2)) + '\n'
fund = 0
for i in self.ledger:
fund += i["amount"]
end_string += "Total: {}".format(round(float(fund),2))
return end_string
#this function hasn't been finished
def create_spend_chart(categories):
#creating the totaling with driven amount
total = 0
for i in categories:
total += i.withdrawals
#looking for the amount of 'o'
list_amount_of_o = []
for i in categories:
percentage = (i.withdrawals/total)*100
amount_of_o = percentage // 10
if percentage%10 > 5:
amount_of_o += 1
list_amount_of_o.append(amount_of_o)
I have been clicking on the run button but these are the result I have been getting, if i change main.py the test results give no failures except those that are supposed to be failing because I haven’t completed it yet.
If I change for example the main.py 8th line from ‘food.deposit(1000, “initial deposit”)’ to
‘food.deposit(900, “deposit”)’ the failure doesn’t show up.
These are class variables and not instance variables.
I would look up what that means on Google. Lots of people have written good articles that explain it better than I can in one forum post.