0

I'm trying to check user-input against the list I created inside a function. What is the best way to pass variables between functions without using globals?

def counter():
    integers = []
    x = 0
    while x < 100:
        x = x + 1
        integers.append (x)
        #print integers
    return

integers_global = counter () 
print integers_global
speed = int (raw_input ("what is the speed of an unladen swallow in km/h (enter a number): "))
if speed in integers_global:
    print "the speed of the swallow is equal to", speed, "km/h"
else:
    print "the speed of the swallow is equal to %r" %speed

2 Answers 2

1

What is the best way to pass variables between functions without using globals?

arguments to get values into functions, return values to get them out. So in your case you only have the second problem to solve, and should just change the bare return (which returns a value of None) into return integers (which returns the result value you care about!-).

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

Comments

0

The obvious thing to do is to return the list you created. Just modify the return statement to

 return integers

at the end of your counter() function.

If you were using those in a class and wanted integers to be used many times or across different methods for some reason, you could also make it a data attribute like self.integers but this is not the case here.

Comments

Your Answer

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