2

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.

2
  • 1
    Why do you think your code isn't working? I ran it with positive and negative test cases and it passed: Let's find an integer divisible by 3. Enter an integer: 3 3 is divisible by 3. Isn't that terrific. Try again! Let's find an integer divisible by 3. Enter an integer: 6 6 is divisible by 3. Isn't that terrific. Try again! Let's find an integer divisible by 3. Enter an integer: 5 5 is not divisible by 3. Boo hoo. Try again! Let's find an integer divisible by 3. Enter an integer: Commented Sep 5, 2015 at 17:38
  • @ShawnMehan Oh, yeah, I guess I wasn't clear about that. I wanted the program to keep asking for more numbers until there's one that's divisible by three. It's supposed to end once there's a number divisible by three. Commented Sep 5, 2015 at 19:26

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Or you can specify global a in the function.
@Alexander: He doesn't appear to have that bad habit; let's keep it that way.
Thanks for the comment! I'm having trouble, though, because I'm dealing with variables I don't know the values of—they need input from the user. And I can't just say "placeholder = var" because one of them is outside the function. Ugh, there's just so much I don't know.
4

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.

1 Comment

Okay, thanks! That's helpful. I modified the code to be more direct: switch = by_three(int(var)) I'm still having trouble getting the value of by_three(var) to resolve to True or False. I thought I could do that from inside the If statement, but I can't get it to work.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.