How can local variables in Python be converted to global variables?
To make a local variable into a global variable, you can declare the variable as global using the global keyword. Variables declared within a function using the global keyword will continue to exist after the function has finished executing, allowing them to be accessed and modified outside of the function.
Here is an example:
def func():
global x
x = 10
func()
print(x) # 输出 10
In this example, x is declared as a local variable within the func() function. By using the global keyword to declare x as a global variable within the function, x will still exist and can be accessed from outside the function after it is executed.