Hey people!
Actually there isn’t any error (as far as I can tell). It is just that it took me sometime to define the create_spend_chart for the budget exercise, and I finally got it.
But I think there should be a more understandable/short/fast way to do it. I just wasn’t able to come with it.
If you can share the way you did it, that would help me understand and maybe find a better way to solve these kinds of problems.
Thank you, and have a great day!
def create_spend_chart(categories):
text = ["100| ", " 90| ", " 80| ", " 70| ", " 60| ", " 50| ", " 40| ", " 30| ", " 20| ", " 10| ", " 0| "]
d = dict()
total = float()
for item in categories:
y = re.findall(’*([a-zA-Z]+)’, str(item))
for line in str(item).split("\n"):
if re.search('^Transfer', line) or re.search('-([0-9.]+)', line) is None:
continue
x = re.findall('-([0-9.]+)', line)
total = total + float(x[0])
try:
d[y[0]] = d[y[0]] + float(x[0])
except:
d[y[0]] = float(x[0])
for value in d.values():
z = 10
w = 9
percentage = (value / total) * 10
if percentage == z:
text[0] = text[0] + "o "
pass
else:
while not w < percentage < z:
z = z - 1
w = w - 1
while w > -1:
text[10 - w] = text[10 - w] + "o "
w = w - 1
while z < 11:
text[10 - z] = text[10 - z] + " "
z = z + 1
text.append(" " + "-" * (len(text[10]) - 4))
for key in d.keys():
i = 12
for c in key:
try:
text[i] = text[i] + c + " "
except:
text.append(" " * 5 + c + " ")
if (i - 1) == 11:
pass
elif len(text[i]) < len(text[i - 1]):
text[i] = text[i].rjust(len(text[i - 1]), " ")
i = i + 1
if len(text[-1]) < len(text[i-1]):
for idx in range(len(text)):
if len(text[idx]) < len(text[12]):
text[idx] = text[idx].ljust(len(text[12]), " ")
for idx in range(len(text)):
if len(text[idx]) < len(text[11]):
text[idx] = text[idx].ljust(len(text[11]), " ")
t = "Percentage spent by category\n" + "\n".join(text)
return t
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36
Challenge: Budget App
Link to the challenge: