3

I'm very new to Python (~8 hours of experience and study in a span of two days) and doing some self-created exercises to practice newly learned information. Currently focusing in classes and functions. The code is regarding selling fruits: there is a class fruit, which takes name, color, price_usd and its objects. There is also a "shopping cart" list and total_cost (initially set as 0). I have made the code work properly, however now trying to not repeat code using functions.

I created a function called "buy", which prints which fruit was chosen, adds it to the shopping cart and add its value to total_cost. However, I am getting "Unresolved reference" for total_cost (but not for shopping cart, which is also a variable outside the function?)

Error message: "UnboundLocalError: local variable 'total_cost' referenced before assignment)

(Code shown is a separate file with the problem I am trying to solve to, then, include in the main file. No new variables, though, as main file only had a lot of conditional statements and new objects.)

Tried creating total_cost before and after function. Also tried to 'manually' add fruit_choice.price_usd to total_cost: "total_cost = total_cost + fruit_choice.price_usd" It created local variable "total_cost" and gave out "Unresolved reference" for second total_cost

import fruit as fr


apple = fr.Fruits("Apple", "red", price_usd=0.88)

shopping_cart = []

# total_cost = 0


def buy(fruit_choice):

    print(f"A(n) {fruit_choice.name} has been added to your shopping cart.")

    shopping_cart.append(fruit_choice.name)
    ##############################################
    total_cost += fruit_choice.price_usd

    print(f" The total cost is: {total_cost}")
    ##############################################

total_cost = 0

print(shopping_cart, "\n", buy(apple))

(Between the ###... is where the error message is coming from.)

I expected the fruit_choice.price_usd being added to total_cost. It would be easier if that was the case, as it is the same code for every fruit.

Thank you in advance for your precious help!

2
  • @Daniel A. If my answer resolved your issue please mark it as correct. So the subject can be closed. Commented May 13, 2019 at 0:54
  • Thank you for the tip, furas. Will make sure to do that next time. Commented May 13, 2019 at 1:20

1 Answer 1

1

if you would like to use a variable that was created outside of a function you need to pass it in as a parameter or use the global keyword like so:

Using global keyword:

apple = fr.Fruits("Apple", "red", price_usd=0.88)

shopping_cart = []

# total_cost = 0


def buy(fruit_choice):


    print(f"A(n) {fruit_choice.name} has been added to your shopping cart.")
    global total_cost;
    shopping_cart.append(fruit_choice.name)
    ##############################################
    total_cost += fruit_choice.price_usd

    print(f" The total cost is: {total_cost}")
    ##############################################

total_cost = 0

print(shopping_cart, "\n", buy(apple))

As parameter:

apple = fr.Fruits("Apple", "red", price_usd=0.88)

shopping_cart = []

# total_cost = 0


def buy(fruit_choice,total_cost):


    print(f"A(n) {fruit_choice.name} has been added to your shopping cart.")
    global total_cost;
    shopping_cart.append(fruit_choice.name)
    ##############################################
    total_cost += fruit_choice.price_usd
    print(f" The total cost is: {total_cost}")
    return total_cost
    ##############################################

total_cost=buy(apple)
print(shopping_cart, "\n",total_cost)

The total_cost variables needs to be created before the first call to buy() it is not important if it was created before or after definition of buy()

Sign up to request clarification or add additional context in comments.

2 Comments

you could also add example which shows how to pass it as parameter - total_cost = buy(total_cost, apple)
Thank you very much! I just found out about global variables and I thought if I had used it, it would be overwritten every time I call the function. Does not seem to be the case, so I should study that now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.