r/learnpython • u/9j810HQO7Jj9ns1ju2 • Oct 26 '26
how do i overwrite a variable?
name = "Alice"
def func():
name = "Bob"
func()
print(f"Hello, {name}!") # i want it to print bob
0
Upvotes
View all comments
9
u/Candid_Zebra1297 Oct 26 '26
So functions don't change variables outside of themselves. If you want the function to do something to the 'outside world' (outside the function) it has to return something. So you could do this...
def func():
return "Bob"
If you call it...
func()
Nothing would happen because we haven't used the thing the function gives us. So you would need to do this...
name = func()
Then print.
The problem you are having is with scope.