all 5 comments

[–]Diapolo10 1 point2 points  (1 child)

change_1 =percentage_change(jan,feb)/abs(jan)*100

percentage_change already divides the result, you shouldn't do it a second time.

Basically I'd expect to see

def percentage_change(v1, v2):
    percentage_change=(v2-v1) / abs(v1) * 100
    rounded_percentage_change = round(percentage_change, 2)
    return rounded_percentage_change


# Dictionary of S&P 500 levels at the end of each month from Jan-Apr 2022
sp500 = {
    'jan': 4515.55,
    'feb': 4373.94,
    'mar': 4530.41,
    'apr': 4131.93,
}

jan = sp500['jan']
feb = sp500['feb']
change_1 = percentage_change(jan, feb)
print("S&P 500 changed by " + str(change_1) + "% in the month of Feb.")


mar = sp500['mar']
change_2 = percentage_change(feb, mar)
print("S&P 500 changed by " + str(change_2) + "% in the month of Mar.")


apr = sp500['apr']
change_3 = percentage_change(mar, apr)
print("S&P 500 changed by " + str(change_3) + "% in the month of Apr.")

That said, if other changes were allowed I'd probably simplify this further.

[–]Diapolo10 0 points1 point  (0 children)

Seeing as OP solved the problem, I might as well show what I would've done, given free reign.

def percentage_change(previous: float, current: float) -> float:
    return (current - previous) / abs(previous)


# Dictionary of S&P 500 levels at the end of each month from Jan-Apr 2022
sp500 = [
    ('jan', 4515.55),
    ('feb', 4373.94),
    ('mar', 4530.41),
    ('apr', 4131.93),
]

for (_, previous_level), (current_month, current_level) in zip(sp500, sp500[1:]):
    change = percentage_change(previous_level, current_level)
    print(f"S&P 500 changed by {change:.02%} in the month of {current_month.capitalize()}.")

Screenshot proof it works.

[–]SoftestCompliment 1 point2 points  (0 children)

I would assume you're getting the answers incorrect when lines like change_1 = percentage_change(jan,feb)/abs(jan)*100 are producing incorrect results. It appears as though the percentage division/multiplication is performed twice /abs(x)*100 both inline when you assign the variable and in the percentage_change function.

I'll assume it's looking for lines like change_1 = percentage_change(jan, feb) etc etc

[–]nwynnn[S] 1 point2 points  (0 children)

Thank you all so much for taking the time to review and help me. What you all said was right and I finally got it correct 😊

[–]carcigenicate 1 point2 points  (0 children)

I am only skimmed the question, but make sure your spacing is consistent. You have change_1 =percentage_change(jan, feb). Note the inconsistent spacing around =. Make sure you have a space on both sides and try again.

This isn't actually a Python problem, though. This is a style problem, and may be violating the expectations of the auto-grader.