0

I'm trying to pass information back and forth between 2 scripts. In one, we obtain a user input, in the other some modification is done to the user input, then back to the first one, we print out that modification.

#del2

def fun():
    return int(user_input)+1

#script to run

user_input=input('some number')
from del2 import fun

print(fun())

So when we run our script, the user gives some input, the next line then runs the other script, which adds a value of 1 to the user inputted value, and then we print out that modified value. However, it appears you can't define a variable in one script, and have that defined variable transfer over to another script. Thus, I get this error when I try the above: NameError: name 'user_input' is not defined. I've tried to look at other posts regarding this, but they use tkinter and all are a bit too complicated/over my head to understand. So I made a very basic simple example to try and understand how this all works.

Edit: I don't want to make another post, since its regarding the same issue. If I have to define every input used for every function, then it becomes quite crowded if you have multiple inputs. I.E.

#del2
def fun(user_input):
    return int(user_input)+1

def fun2(user_input2):
    return int(user_input2)+1

def fun3(user_input3):
    return int(user_input3)+1

def fun4(user_input4):
    return int(user_input4)+1

def fun5(user_input,user_input2,user_input3,user_input4):
    return fun(user_input)+fun2(user_input2)+fun3(user_input3)+fun4(user_input4)

#script to run

user_input=input('some number')
user_input2=input('some number')
user_input3=input('some number')
user_input4=input('some number')
from del2 import fun5

print(fun5(user_input,user_input2,user_input3,user_input4))

Is there a better way to do this, so fun5 doesn't become extremely long if you have multiple inputs.

samman
  • 559
  • 10
  • 29
  • 2
    You need to define fun so it takes the variable as a parameter: `def fun(user_input)` then pass that variable to the imported function. – Stuart Jun 27 '20 at 23:06
  • what if you have multiple inputs? Can you just separate them with a comma? – samman Jun 27 '20 at 23:07
  • 2
    Yes you specify all the parameters you want to pass, separated by commas. You might want to work through a tutorial on python functions and variable scope before starting on modules. – Stuart Jun 27 '20 at 23:10
  • 1
    What is scripts - do you mean two process/programs running separatly? Or it is same program and you don know how to make global variable? – Chameleon Jun 27 '20 at 23:13
  • Sorry one other question: If I have multiple inputs, do I need to add them all to my function? Even if I used it in the previous function? I.E. If I add this```def fun2(): return fun(user_input)+1``` Then it won't work, I'll have to add ```def fun2(user_input): return fun(user_input)+1```. Even though fun2 is not using the user_input, its using the return value of fun(user_input). What do I do if I have one function calling in multiple functions, each using a different user input. Do I have to indicate in that function every single user input used? – samman Jun 27 '20 at 23:26

2 Answers2

1

You need to define fun so it takes the variable as a parameter: def fun(user_input) then pass that variable to the imported function.

Also if you want user_inputs value to change after you call your fun() function you need to something like this:

#del2

def fun(user_input): 
    return int(user_input) + 1

#script to run

user_input = input('some number')
from del2 import fun

user_input = fun(user_input)
print(user_input)

Edit: The fun() function isnt for just user_input. So you can use the same fun() function for another variables.

#del2

def fun(any_input): # i changed the variables name just to be clear
    return int(any_input) + 1

#script to run

user_input = input('some number')    
user_input2 = input('some number')

from del2 import fun


user_input = fun(user_input)
user_input2 = fun(user_input2)
print(user_input + ", " + user_input2)

and you can add the input variables to an array and do something like

#del2

def fun(any_input): 
    return int(any_input) + 2
def fun1(any_input): 
    return int(any_input) * 2
def fun2(any_input): 
    return int(any_input) // 2 
def fun3(any_input): 
    return int(any_input) - 2

def fun5(input_array): 
    functions = [fun, fun1, fun2, fun3]
    final = 0
    if len(input_array) != 4:
        raise Exception("Not enough functions for inputs")
    for i in range(len(input_array)):
        final += functions[i](input_array[i])
    return final

#script to run

user_inputs = []
user_inputs.append(input('some number 0: ')) #you can use a for loop here too
user_inputs.append(input('some number 1: '))
user_inputs.append(input('some number 2: '))
user_inputs.append(input('some number 3: '))

from del2 import fun5

user_inputs = fun5(user_inputs)
print(user_inputs)
TUNAPRO1234
  • 106
  • 5
  • This only works because modifications to the inputs are all the same. I'm looking for something a bit more flexible (i.e. what if one was adding, another was dividing, etc. The issue isn't what are the aboves doing, the issue is fun5 uses the output of all the previous functions, but I want a way to use fun5 without defining every input the previous functions used (i.e. I don't want fun5(user_input,user_input2). Again your solution only works because each function is doing the same thing, if we change up what each function is doing, then it wouldn't work anymore. – samman Jun 28 '20 at 00:21
  • My english is not good enough to explain but there is some other thing that you can use, `global` keyword – TUNAPRO1234 Jun 28 '20 at 00:27
  • I've tried to define global in both scripts, but global parameters don't seem to transfer over (i.e. you'd still need to define it the entry of the fun) – samman Jun 28 '20 at 00:29
  • if you have a spesific length on functions and inputs, then you can add the diffrent fun functions into an array then use the array in the for loop – TUNAPRO1234 Jun 28 '20 at 00:35
0

You can do this using the global keyword and a global variable within the imported module that is accessed by the different functions. Taking a simpler example that just adds or subtracts from a globally stored total:

# tally.py
total = 0

def add(n):
    global total
    total += n

def subtract(n):
    global total
    total -= n

# test_tally.py
import tally
tally.add(5)
tally.subtract(1)
print(tally.total)

However, global variables are bad. We do not generally think in terms of passing data back and forth between modules. The imported module is executed in its entirety at the time of importing, so data can only be passed to functions or other objects within the imported module.

Instead, modules often include classes, which can be used to generate objects that can store state. Data can be passed to these objects and stored within them. The objects can then operate upon the data and return different results by calling different methods of the object. This would be written like this:

# tally.py
class Tally(object):
    def __init__(self):
        self.total = 0
        
    def add(self, n):
        self.total += n
        
    def subtract(self, n):
        self.total -= n

# test_tally.py
from tally import Tally
tally = Tally()
tally.add(3)
tally.subtract(4)
print(tally.total)
Stuart
  • 9,597
  • 1
  • 21
  • 30