I'm a beginner programmer and I am trying to understand the difference between these three functions. Originally my problem is a bit different but I made it this easy for not to take your time a lot. I actually learned exec function today and no matter how much I try different variations with the function, I can't understand the difference between these v1 and v2. I want to use the v1 version but it doesn't change the variable p5. If anyone can clarify me I would be so glad.
NOTE: Original problem have p1,p2,p3,...,p9 and I get the index from the user input. So it is important to formatting string.
p5 = 5
index_of_p = 5
def change_p5_v1():
exec(f"global p{index_of_p}")
exec(f"p{index_of_p} = 3")
print(p5) # Output 5
def change_p5_v2():
exec("global p5")
exec("p5 = 3")
print(p5) # Output 5
def change_p5_v3():
global p5
p5 = 3
print(p5) # Output 3
change_p5_v1()
change_p5_v2()
change_p5_v3()