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.