如何将变量从一个函数传递到另一个函数并在 python 的条件语句中使用它们?

How do I pass variables from one function to another and use them in conditional statements in python?

我是一般编程的新手,甚至是 python 的新手。我只有几天的时间。我正在解决一个我知道很简单的问题,但我对它的修改越多,它似乎就越糟糕。

基本上我想做的是从函数 a 中的条件创建一个变量,我可以将其传递给其他函数。我一直在尝试这样做,以创建世界上最简单的角色扮演游戏中的角色创建屏幕。然而,就像游戏一样简单,我很快就陷入了困境。我决定保持我所追求的精神,但举一个更简单的例子。

现在,我只是想拼凑一顿饭。

def entree():
    options = "1) steak\n2) burger"
    print(options)
    entree_selection = int(input("Pick one."))
    if entree_selection == 1:
        entree_choice = "steak"
        side_dish()
    elif entree_selection == 2:
        entree_choice = "burger"
        side_dish()


def side_dish():
    entree_choice = entree()
    print(f"You chose {entree_choice}.")
    print("Now choose your side.")
    options = "1) baked potato\n2) green beans"
    print(options)
    side_selection = int(input("Pick one."))
    if side_selection == 1:
        side_choice = "baked potato"
        dessert()
    elif side_selection == 2:
        side_choice = "green beans"
        dessert()


def dessert():
    entree_choice = entree()
    side_choice = side_dish()
    print(f"So far we have {entree_choice} and {side_choice}.")
    print("How about dessert?")
    options = "1) cake\n2) ice cream"
    print(options)
    dessert_selection = int(input("Pick one."))
    if side_selection == 1:
        dessert_choice = "cake"
        your_meal()
    elif side_selection == 2:
        dessert_choice = "ice cream"
        your_meal()


def your_meal():
    entree_choice = entree()
    side_choice = side_dish()
    dessert_choice = dessert()
    print(
        f'You will be having {entree_choice} with {side_choice} and {dessert_choice}')


entree()
side_dish()
dessert()
your_meal()

我的问题是 函数 a 一遍又一遍地重复 运行 函数 b

老实说,我已经忘记了我尝试过的所有事情。我至少尝试了 10 种来自 YouTube 的东西,至少尝试过 10 种来自这里的东西。

您缺少的是函数 'return value' 的概念。首先将您的任务分解为离散的单元。使用您的 your_meal 示例,程序将首先 运行 函数 entree() 并将该函数的结果设置为变量 entree_choice。但要使其工作,函数 entree 需要有一个(或多个)return 'steak' 或类似类型的语句。这就是程序的不同部分如何相互“交流”:通过参数和函数的 return 值。

另请注意,如果您调用 entree,然后 entree 调用 side_dish 并且 side_dish 然后调用 entree,那么程序将永远不会停止循环(这就是您描述的问题)作为正在发生。

函数 A 重复,因为在函数 A 的末尾调用函数 B,在函数 B 的开头调用函数 A。这会重新启动循环。

函数 B 始终是 运行,它永远无法完成,因为您已请求函数 B 始终重新运行函数 A。

为了不重复,不能要求side_dish重新运行entree

def side_dish():
    entree_choice = entree() # this is the problem
    print(f"You chose {entree_choice}.")
    print("Now choose your side.")

相反,将 entree_choice 作为变量传递给您的 side_dish

def side_dish(entree_choice):
    print(f"You chose {entree_choice}.")
    print("Now choose your side.")

要继续该示例,您将创建一个甜点函数,它接受您之前的两个选择。


def dessert(entree_choice, side_choice):
    print(f"So far we have {entree_choice} and {side_choice}.")
    print("How about dessert?")

这很自然地让我们进入最终函数:


def your_meal(entree_choice, side_choice, dessert_choice):

    print(
        f'You will be having {entree_choice} with {side_choice} and {dessert_choice}')


综上所述,我建议采用略有不同的实施方式。基本上,有一个“控制器”。这个东西在一个级别上控制所有逻辑。

entree_choice = entree()
side_choice = side_dish()
dessert_choice = dessert()
print(f'You will be having {entree_choice} with {side_choice} and {dessert_choice}')

为了完成这个,两件事:

  1. 将您的基础 entree 函数修改为 询问边选择。而是让控制器询问。
  2. Return他们选择的主菜。
def entree():
    options = "1) steak\n2) burger"
    print(options)
    entree_selection = int(input("Pick one."))
    if entree_selection == 1:
        entree_choice = "steak"
        return entree_choice
    elif entree_selection == 2:
        entree_choice = "burger"
        return entree_choice