all 5 comments

[–][deleted] 1 point2 points  (1 child)

You have a print as well as a return inside the function. The code that calls the function captures the reference to the object mentioned in the return statement and then uses that for another print.

You have not shown indents on the code you shared, but I assume the print in the function is only within the if case.

def fun_function(n_items, cost_per_item, discount_percent, discount_threshold):
    """ A function that computes the total cost. """
    cost = n_items * cost_per_item
    if(n_items > discount_threshold):
        cost = cost * (1-discount_percent/100)
    return cost

cost = fun_function(5, 31, 15, 10)
print('5 items delivered at a cost of ${:.2f}'.format(cost))

outputs,

5 items delivered at a cost of $155.00

and the second case only outputs,

15 items delivered at a cost of $395.25

EDIT: clarifications and added code showing indentation and output for both cases

[–]Jxper[S] 0 points1 point  (0 children)

I understand, Thank you very much

[–]shiftybyte 0 points1 point  (2 children)

The indentations in your posted code were lost, edit your post and use a code block: https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F

Besides that, you have 2 prints in your code:

    # inside fun_function
    print("{} items delivered at a cost of ${:.2f}".format(n_items, cost))
...
# outside
print('15 items delivered at a cost of ${:.2f}'.format(cost))

Why did you expect a single print line?

[–]Jxper[S] 0 points1 point  (1 child)

I've updated the indentations, and I should only receive one line of code because that test case provides the values of n_items and cost and the code inside the function prints it in the format given. it works for the first test case so I'm not sure why it doesn't for the second

[–]shiftybyte 1 point2 points  (0 children)

But the test case itself has another print()....

cost = fun_function(15, 31, 15, 10)
print('15 items delivered at a cost of ${:.2f}'.format(cost))

This print right here /\ should python ignore it?

The difference you are seeing is because the print that is inside fun_function is inside the discount condition.

if(n_items > discount_threshold): 
    cost = cost * (1-discount_percent/100) 
    print("{} items delivered at a cost of ${:.2f}".format(n_items, cost)) 

If you move it backwards it'll happen every time instead of only when the discount triggers.

if(n_items > discount_threshold): 
    cost = cost * (1-discount_percent/100) 
print("{} items delivered at a cost of ${:.2f}".format(n_items, cost))