-1

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?

Idos
  • 15,053
  • 14
  • 60
  • 75
Jess
  • 71
  • 3
  • 10
  • 1
    You can't return twice. Your function ends with the first return statement that is reached. – timgeb Jan 29 '16 at 14:33
  • I've changed to return "price" only, but it still prints "0".How to solve it? – Jess Jan 29 '16 at 14:45
  • 1
    @Jess you have to describe exactly the problem you are having, the input and expected output for us to be able to help you – Idos Jan 29 '16 at 14:46

2 Answers2

3

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)
Idos
  • 15,053
  • 14
  • 60
  • 75
0

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()
Vahid Msm
  • 1,032
  • 7
  • 16
  • I removed the global variables, but it results in error (NameError: name 'price' is not defined) – Jess Jan 29 '16 at 14:52
  • @Jess The indent of this line ```PrintDetails(price,TotalPrice,Balance)```was incorrect, now try the code. – Vahid Msm Jan 29 '16 at 14:56
  • I added after your remind, but it still pops up the same error – Jess Jan 29 '16 at 14:58
  • @Jess I updated the code, you don't need pass variables to your ```PrintDetails``` function since it's getting ```price``` and ```TotalPrice``` values from ```SumPrice``` function. Also i assume that you defined ```cup_cone``` , ```Member_Ans``` and ```cone_size ``` somewhere in your code. – Vahid Msm Jan 29 '16 at 15:04
  • I follow all the codes that u have changed for me, now another error occured. UnboundLocalError: local variable 'price' referenced before assignment – Jess Jan 29 '16 at 15:14
  • @Jess Where it happens? – Vahid Msm Jan 29 '16 at 15:16
  • I solved it! Thanks so much for your help!!! But now i face another problem, in this line print ("Price after 10% discount = RM %.2f" % (TotalPrice)) , it shows 'TypeError: not enough arguments for format string', can you help me? – Jess Jan 29 '16 at 15:24
  • @Jess Great. Your error's because of your ```10%``` string. Python uses the ```%``` character for string formatting. instead use ```10%%``` , I updated the code. – Vahid Msm Jan 29 '16 at 15:39