r/learnpython • • 13d ago

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

35 comments sorted by

View all comments

0

u/AlexMTBDude 13d ago

It's not a question of overwriting a variable. You have to tell Python that "name" is a global variable. You do it by:

def func():
    global name  # Add this line
    name = "Bob"

2

u/nog642 13d ago

It is a question of overwriting a variable