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

发布于 2025-01-15 20:01:44 字数 1763 浏览 0 评论 0原文

我对一般编程很陌生,对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

说实话,我已经忘记了我尝试过的所有事情。我已经从 YouTube 尝试了至少 10 种方法,并且从这里尝试了至少相同的方法。

I'm new to programming in general and even newer to python. I'm only a couple of days into it. I'm working on a problem that I know is simple, but the more I tinker with it, the worse it seems to get.

essentially what I am trying to do is create a variable from a conditional in function a that I can pass to other functions. I've been trying to do this to create like character creation screens in the world's simplest RPG. However, as simple as the game is, I quickly got in over my head. I decided to keep the spirit of what I'm after, but with an easier example.

Now, I'm simply trying to put a meal together.

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

The issue for me is that function a is repeating over and over without ever running function b

To be honest, I've lost track of all the things I have tried. I've tried at least 10 things from YouTube and at least the same number from here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人│生佛魔见 2025-01-22 20:01:44

函数 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. 返回他们选择的主菜。
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

Function A is repeating because at the end of Function A, you call Function B, and at the beginning of Function B, you call Function A. Which restarts your loop.

Function B is always running, it just can't ever finish because you have requested that Function B always reruns Function A.

In order to not repeat, you cannot request side_dish to rerun entree.

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

Instead, pass the entree_choice as a variable into your side_dish.

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

To continue the example, you would then create a dessert function which takes in both of your previous choices.


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

Which naturally gets us to the final function:


def your_meal(entree_choice, side_choice, dessert_choice):

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


With all of that being said, I would recommend a slightly different implementation. Basically, have a "controller". This thing controls all of the logic at one single level.

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

In order to accomplish this, two things:

  1. Modify your underlying entree function to not ask about the side choice. Instead let the controller ask.
  2. Return the entree that they chose.
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
迎风吟唱 2025-01-22 20:01:44

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

另请注意,如果您调用 entree,而 entree 然后调用 side_dish,side_dish 然后调用 entree,那么程序将永远不会停止循环(这是您所描述的问题)。

What you're missing is the idea of the 'return value' of a function. Start by breaking your task into discrete units. Using your example of your_meal, the program will run the function entree() first and set the result of that function to the variable entree_choice. But for it to work, the function entree needs to have one (or several) return 'steak' or similar type statements. This is how the different parts of your program "communicate" with each other: by way of arguments and return values from functions.

Also note that if you call entree, and entree then calls side_dish and side_dish then calls entree, then the program will never stop looping (which is the problem you've described) as happening.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文