Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading