简体   繁体   中英

Python passing variables between functions (boolean)

EDIT: Ok, after some input from everyone here, I managed to make it work! I simplified it a lot to get rid of the long chains. Here's what I have:

def main():
    var = (raw_input("Enter an integer: "))
    a = get_number(var)
    if a != False:
        switch = by_three(int(a))
        if switch == True:
            return
        else:
            print "Try again!"
            main()
    else:
        main()


def get_number(parameter):
    if parameter.isdigit():
        return parameter
    else:
        print "No, I said an INTEGER. \"%s\" is not an integer." % parameter
        return False


def by_three(placeholder):
    if placeholder % 3 == 0:
        print "%d is divisible by 3. Isn't that terrific." % placeholder
        return True
    else:
        print '%d is not divisible by 3. Boo hoo.' % placeholder
        return False

print "Let's find an integer divisible by 3."
main()

Is there any reason I shouldn't go back to main() in my else statements? Is there another way to get the program to go back to the beginning?

——— I tried to build a simple command-line program for finding numbers divisible by 3. The idea is to keep asking for a number until the user picks one that's divisible by three. Here's my code:

def main():
    print "Let's find an integer divisible by 3."
    var = (raw_input("Enter an integer: "))
    switch = False
    get_number(var, switch)
    while switch != True:
        print "Try again!"
        main()

def get_number(parameter, nd):
    if parameter.isdigit():
        by_three(int(parameter), nd)
        return parameter, nd
    else:
        print "No, I said an INTEGER. \"%s\" is not an integer." % parameter
        return parameter, False


def by_three(placeholder, tf):
    if placeholder % 3 == 0:
        print "%d is divisible by 3. Isn't that terrific." % placeholder
        return placeholder, True
    else:
        print '%d is not divisible by 3. Boo hoo.' % placeholder
        return placeholder, False

main()

OK, so here's what I thought was happening: variable switch gets passed to nd , which gets passed to tf . If the other variable (which goes var>parameter>placeholder) IS divisible by three, there's a return of True for tf —which should mean that the variable is now changed when I test it with "while."

That must not be what's happening—can someone explain how I'm misunderstanding things so badly? Passing variables around functions (and returning them!) is pretty confusing to me.

Let us focus on this portion of your code:

...
switch = False
get_number(var, switch)
while switch != True:
   ...

What you are doing in this code snippet:

You have a variable, switch , which is False in the beginning. You send that to a function, and you check if it is True or not afterwards.

Why this does not makes sense?

Let's consider the following program:

def changeVariable(a)
   a = a * 2

a = 5
changeVariable(a)
print(a)

This program still prints 5 , because basically stated : What you do in a function, stays in the function.

So, in your case, calling get_number(var, switch) and checking the value of switch does not make sense, because it will be False all the time .

Solution:

You can use return statements. Then, you will have the output of your function.

Example:

def changeVariable(a)
   a = a * 2
   return a

a = 5
result = changeVariable(a)
print(a)
print(result)

The above code will print 5 and then 10 .

When you define a function, the value that you return via return value is given to the function itself, not the arguments passed to the function.

For example,

def f(i,j):
  i=1
  j=2
  return i,j

i=3
j=4
i,j=f(i,j)

will give i=1 and j=2, but

f(i,j)

will leave i and j unmodified (i=3 and j=4) because the scope of the variables inside the function is local.

so tf isn't used anywhere. There is a value passed into the by_three method, but then it isn't used in the method and hence isn't available to be returned. Right now, switch -> nd -> tf. And by_three accepts tf but doesn't put it to work.

by_three is correctly setting True or False for your problem, and passing it back up the chain to nd -> switch where it is being used in the conditional while correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM