如何将在局部范围内给出的值传输到 python 中的全局值
How to transfer a value that was given in a local scope to a global one in python
举个例子:
H = 21
def fun():
H = input("Assume the input is 1")
print(H)
>>> 21
如何使用户输入的 H = 1?
有时我希望本地范围移动到全局范围,以便我可以将它们用作其他功能的占位符。除非有更好的方法,否则我也想帮忙,谢谢!
在函数内部将H定义为全局。尝试:
H = 21
def fun():
global H
H = input("Assume the input is 1")
调用函数:
fun()
print(H)
如果要分配 H
一个整数,请不要忘记使用 int(input("Assume the input is 1"))
。
Return
也有帮助:
H = 21
def fun():
return input("Assume the input is 1")
现在打电话:
H = fun()
print(H)
举个例子:
H = 21
def fun():
H = input("Assume the input is 1")
print(H)
>>> 21
如何使用户输入的 H = 1? 有时我希望本地范围移动到全局范围,以便我可以将它们用作其他功能的占位符。除非有更好的方法,否则我也想帮忙,谢谢!
在函数内部将H定义为全局。尝试:
H = 21
def fun():
global H
H = input("Assume the input is 1")
调用函数:
fun()
print(H)
如果要分配 H
一个整数,请不要忘记使用 int(input("Assume the input is 1"))
。
Return
也有帮助:
H = 21
def fun():
return input("Assume the input is 1")
现在打电话:
H = fun()
print(H)