Return value doesn't work in Python 3

Question:

It seems like I can’t pass a value from a function to another even though I have put a return statement in the 1st function.

This is my code:

price=0
TotalPrice=0

def SumPrice(price,TotalPrice):
    if cup_cone=="cup":
        price=(price+(mass/10)*0.59)*TotalSet
    else:
        if cone_size=="small":
            price=(price+2)*TotalSet
        else:
            if cone_size=="medium":
                price=(price+3)*TotalSet
            else:
                price=(price+4)*TotalSet

    if Member_Ans=="yes":
        TotalPrice=TotalPrice+price*0.90

    print(price,TotalPrice)
    return (price)
    return (TotalPrice)

def PrintDetails(price,TotalPrice,Balance):
    SumPrice(price,TotalPrice)
    if Member_Ans=="yes":
        print("Member ID: ", loginID, " (" , Username, ")")

    for element in range (len(UserFlavor)):
        print (UserFlavor[element], "--- ", UserFlavorPercentage[element], "%")

    print ("Total set = ", TotalSet)
    print ("Total price = RM %.2f" % (price))
    if Member_Ans=="yes":
        print ("Price after 10% discount = RM %.2f" %  (TotalPrice))

    while True:
        Payment=int(input("Please enter your payment: "))
        if Payment<TotalPrice:
            print("Not enough payment.")
        if Payment >= TotalPrice:
            break

    Balance=Balance+(Payment-TotalPrice)
    print(Balance)

PrintDetails(price,TotalPrice,Balance)

When I try to print the price and TotalPrice, it prints 0, why?

Asked By: Jess

||

Answers:

You are trying to use return twice, which is not allowed (your function will end as soon as it reaches the 1st return statement, making the other one useless).
You can, however, return both values in one statement:

return (price, TotalPrice)

And then assign the value to a tuple or anything else you would like:

my_tuple = SumPrice(a, b)

or

var1, var2 = SumPrice(a, b)
Answered By: Idos

Your second return statement of first function is not reachable! btw try to not use global variables in your code, instead access return values of your first function.

def SumPrice():
    price = 0
    TotalPrice = 0
    if cup_cone=="cup":
        price=(price+(mass/10)*0.59)*TotalSet
    else:
        if cone_size=="small":
            price=(price+2)*TotalSet
        else:
            if cone_size=="medium":
                price=(price+3)*TotalSet
            else:
                price=(price+4)*TotalSet

    if Member_Ans=="yes":
        TotalPrice=TotalPrice+price*0.90

    return price, TotalPrice

def PrintDetails():
    price, TotalPrice = SumPrice()
    if Member_Ans=="yes":
        print("Member ID: ", loginID, " (" , Username, ")")

    for element in range (len(UserFlavor)):
        print (UserFlavor[element], "--- ", UserFlavorPercentage[element], "%")

    print ("Total set = ", TotalSet)
    print ("Total price = RM %.2f" % (price))
    if Member_Ans=="yes":
        print ("Price after 10%% discount = RM %.2f" %  (TotalPrice))

    while True:
        Payment=int(input("Please enter your payment: "))
        if Payment<TotalPrice:
            print("Not enough payment.")
        if Payment >= TotalPrice:
            break

    Balance=Balance+(Payment-TotalPrice)
    print(Balance)

PrintDetails()
Answered By: Vahid Msm
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.