SalutAToi
SalutAToi

Reputation: 67

Return a variable after having been through several functions

So, I'm coding with Python 3.5.

For context, I am currently writing a program (relatively simple) that uses a menu to store in an instance of a class I defined the options that the user is going to use with that program. To write the menus, I have created several functions and I'm passing from one function to the other, always adding the instance containing the options as an argument. I need to call the menu as a function, and have it return the instance after it goes through the menus.

My code and my problem can be simplified like that :

def spam(eggs):
    if eggs is None:
        bacon()
    return eggs

def bacon():
    eggs = ["spam, spam, spam, egg, and spam"]
    sausage(eggs)

def sausage(eggs):
    eggs.append("spam, spam, spam, spam, spam, spam, baked beans, spam, spam, spam, and spam")
    spam(eggs)

print(spam(None))

What I want is to be able to call spam() and have it return eggs. In that case, which is basically the simplification of my code with what I believes matters in it when it comes to the issue I face, is that calling that function only returns None.

As I'm fairly new to Python, I do not understand why it doesn't work.

I know a possibility would be to make that class instance (represented here by the list eggs) a global variable, execute the function and get it back on another line, but I'd like to find a way to do it without using globals.

Upvotes: 0

Views: 82

Answers (2)

Taylor Iserman
Taylor Iserman

Reputation: 185

Your Code

Let's look at what your code does.

def spam(eggs):
    if eggs is None:
        bacon()
    return eggs

Here is your first problem, bacon() neither prints anything, nor does it modify this version of eggs. Currently, it does not return anything either (it returns None). If it did, it wouldn't matter because you are not doing anything with the value it returns.

def bacon():
    eggs = ["spam, spam, spam, egg, and spam"]
    sausage(eggs)

Here it is unclear what you are trying to do. What is happening is that you are creating a list of one item -- which is the string "spam, spam, spam, egg, and spam". If you wanted to create a list with each of those items in it as separate strings it would be ['spam','spam','spam','egg','spam'] or something similar depending on what you want to do with 'and'.

The next issue is that you are storing it in eggs. However, this is not the same eggs as in spam() (it's a local variable) so any changes you make here will not be reflected in spam() unless you return whatever value you want to spam() where it can be assigned to that version of eggs.

The final issue with this function is the same issue that you had with spam() in that you are not doing anything with the value returned by sausage(eggs) and sausage(eggs) does not print anything -- So nothing is accomplished by running it.

def sausage(eggs):
    eggs.append("spam, spam, spam, spam, spam, spam, baked beans, spam, spam, spam, and spam")
    spam(eggs)

Similar problems in this function as in the previous two. Here, you are taking in the value of eggs that is found in bacon(). However, any modifications to it are ineffective as you do not modify that version and nothing is returned by sausage().

eggs.append(...) has a similar issue as before in that you are simply adding one string to the end of the list, eggs, and not individual items. This may be your intention but the purpose is not clear.

spam() returns a value for eggs but nothing is done with the result. So nothing is accomplished with this statement.

print(spam(None))

A Solution

This solution is an approximation of what you are trying to accomplish.

def spam(eggs):
    if eggs is None:
        return bacon()
    return eggs

def bacon():
    eggs = ["spam", "spam", "spam", "egg", "spam"]
    return sausage(eggs)

def sausage(eggs):
    eggs = eggs + ["spam", "spam", "spam", "spam", "spam", "spam", "baked beans", "spam", "spam", "spam", "spam"]
    return spam(eggs)

print(spam(None))

Upvotes: 1

Christy
Christy

Reputation: 58

The following code should work:

def spam(eggs):
    if eggs is None:
        return bacon()
    return eggs

def bacon():
    eggs = ["bacon"]
    return sausage(eggs)

def sausage(eggs):
    eggs.append("sausage")
    return spam(eggs)

print(spam(None))

You can change the strings as you wish.

Upvotes: 1

Related Questions