Python 无法在函数“中使用变量”;未解析引用“;

Python 无法在函数“中使用变量”;未解析引用“;,python,function,variables,Python,Function,Variables,我对Python非常陌生(大约8个小时的经验和两天的学习时间),并且做一些自创的练习来练习新学到的信息。目前专注于类和函数。 代码是关于销售水果的:有一个类fruit,它使用名称、颜色、价格和它的对象。还有一个“购物车”列表和总成本(最初设置为0)。我已经使代码正常工作,但是现在尝试不使用函数重复代码 我创建了一个名为“buy”的函数,它打印选择的水果,将其添加到购物车中,并将其价值添加到总成本中。然而,我得到的是总成本的“未解决参考”(但不是购物车,这也是函数之外的一个变量?) (显示的代码是

我对Python非常陌生(大约8个小时的经验和两天的学习时间),并且做一些自创的练习来练习新学到的信息。目前专注于类和函数。 代码是关于销售水果的:有一个类fruit,它使用名称、颜色、价格和它的对象。还有一个“购物车”列表和总成本(最初设置为0)。我已经使代码正常工作,但是现在尝试不使用函数重复代码

我创建了一个名为“buy”的函数,它打印选择的水果,将其添加到购物车中,并将其价值添加到总成本中。然而,我得到的是总成本的“未解决参考”(但不是购物车,这也是函数之外的一个变量?)

(显示的代码是一个单独的文件,我正试图解决的问题包括在主文件中。不过,没有新的变量,因为主文件只有很多条件语句和新对象。)

尝试在函数前后创建总成本。还尝试“手动”将水果价格添加到总成本中: “总成本=总成本+水果选择。价格为美元” 它创建了局部变量“总成本”,并给出了第二个总成本的“未解决参考”

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))
(在#####…之间是错误消息的来源。)

我希望水果的价格加在总成本上。如果是这样的话会更容易,因为每个水果的代码都是一样的


提前感谢您宝贵的帮助

如果要使用在函数外部创建的变量,则需要将其作为参数传入,或像这样使用全局关键字:

使用
global
关键字:

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))
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)
作为参数:

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

total\u cost
变量需要在第一次调用
buy()
之前创建。它是在
buy()定义之前还是之后创建的并不重要。

您还可以添加示例,说明如何将其作为参数传递-
total\u cost=buy(苹果公司的total\u cost)
非常感谢!我刚刚发现了全局变量,我想如果我使用了它,每次调用函数时它都会被覆盖。似乎不是这样,所以我现在应该研究一下。@Daniel A。如果我的回答解决了你的问题,请把它标记为正确。所以这个话题可以结束了谢谢你的提示furas。下次一定要这样做。