[英]How to pass local variables from one function into another?
I am trying to make a text-based game but I am having troubles passing some variables from one function to another. 我正在尝试制作基于文本的游戏,但是在将一些变量从一个函数传递到另一个函数时遇到了麻烦。 I figured out how to modify the variable inside a function and return new values to overwrite the original.
我想出了如何在函数内修改变量并返回新值以覆盖原始值。
What I need help with is how to get room1()
and room2()
variables back to something1(x)
and something2(y)
and into main()
to unlock the if
statement. 我需要什么帮助是如何获得
room1()
和room2()
变量返回something1(x)
和something2(y)
和成main()
来解锁if
语句。
Should I have two different or one function for something1(x)
and something2(y)
? 我应该为
something1(x)
和something2(y)
使用两个不同的函数还是一个函数?
This is a general sample code with the problem I'm having: 这是我遇到的问题的一般示例代码:
def something1(x):
x += 0
return x
def something2(y):
y += 0
return y
def main():
print("1. Try to open door")
print("2. Go to room1")
print("3. Go to room2")
choice = int(input("Enter selection: ")
if choice == "1":
# Trying to get this if statement to work with the variables
# Don't know which function or parameters to pass in order to get it to work
if x == 3 and y == 2:
print("You're free")
else:
print("You're not free")
elif choice == "2":
room1()
elif choice == "3":
room2()
else:
print("ERROR")
main()
def room1():
print("1. Push thing1")
print("2. Push thing2")
print("3. Push thing3")
print("4. Return to previous room")
pushChoice = input("Enter selection: ")
if pushChoice == "1":
print("Thing1 pushed")
room1()
elif pushChoice == "2":
print("Thing2 pushed")
room1()
elif pushChoice == "3":
print("Thing3 pushed")
# The modified variable x for something1(x)
x = 3
x = something1(x)
room1()
elif pushChoice == "4":
main1()
else:
print("ERROR")
room1()
def room2():
print("1. Pull thingA")
print("2. Pull thingB")
print("3. Pull thingC")
print("4. Return to previous room")
pullChoice = input("Enter selection: ")
if pullChoice == "1":
print("ThingA pushed")
room1()
elif pullChoice == "2":
print("ThingB pushed")
# The modified variable y for something2(y)
y = 2
y = something1(y)
room1()
elif pullChoice == "3":
print("ThingC pushed")
room1()
elif pullChoice == "4":
main1()
else:
print("ERROR")
room1()
You could pass
a variable from one function to another by returning the variable. 您可以
pass
返回变量将变量从一个函数pass
给另一个函数。 However, in order to do that, the function has to call another function within the function body, for example: 但是,为此,该函数必须在函数体内调用另一个函数,例如:
def addandsquare(x, y):
y = squarefunction(x+y) # sum x+y is passed to squarefunction, it returns the square and stores it in y.
return y
def squarefunction(a):
return (a*a) # returns the square of a given number
print(addandsquare(2, 3)) # prints 25
However, if you cannot call a function within it's body, but you would like to use a local variable of that function, you can then declare that variable global to both the functions. 但是,如果您不能在函数体内调用函数,而是想使用该函数的局部变量,则可以将该变量声明为两个函数的全局变量。
Here is an example of that: 这是一个例子:
globvar = 0
def set_globvar_to_one():
global globvar # Needed to modify global copy of globvar
globvar = 1
def print_globvar():
print globvar # No need for global declaration to read value of globvar
set_globvar_to_one()
print_globvar() # Prints 1
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.