Reputation: 1
I am using python 3.1 on a windows 10 device and I have ran into a problem.
When I go to use a assignment that was defined in another function that I made, the assignment does not work. My problem is in a long line of code but I made a smaller version to help explain what is happening.
def test():
""" takes input """
f = input("1 or 2? ")
if f == 1:
t = "wow"
if f == 2:
t = "woah"
def test2(t):
""" Uses input """
print(t)
def main():
test()
test2(t)
main()
input("\n\nPress enter to exit" )
I am not sure why the program wont use the assignment "t" after it selects an input.
My goal is to use the input from the 1st function to change the outcome of the second function. of course my original program is more complicated that a simple print function but this demonstration is what I know is messing up my program. My original program deals with opening .txt files and the input is choosing which file to open.
Any help would be greatly appreciated.
Upvotes: 0
Views: 41
Reputation: 355
You have to return "t" in order to use it in test2:
def test():
""" takes input """
f = input("1 or 2? ")
if f == '1':
t = "wow"
if f == '2':
t = "woah"
return t # This returns the value of t to main()
def test2(t):
""" Uses input """
print(t)
def main():
t = test() # This is where the returned value of t is stored
test2(t) # This is where the returned value of t is passed into test2()
main()
input("\n\nPress enter to exit" )
Upvotes: 1