This is my script:
fruit = "apple"
phrase = "I like eating " + fruit + "s."
def say_fruit(fruit):
print phrase
say_fruit('orange')
I’m trying to get say_fruit to use the string given inside the phrase variable, which actually uses the variable already assigned before to it (apple). How can I achieve this?
In your code,
phraseis bound to a string when the module loads and is never changed. You need to be dynamic, like this:Globals are just a bad idea that will haunt you in the future. Resist the temptation to use them.