Search code examples
pythonreturn

Return value doesn't work in Python 3


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?


Solution

  • 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()