Here is one example of code that does what you are asking about. It uses the global
keyword and eliminates the storage
argument in the farm()
function.
import time
global storage
storage = 0
storage_limit = 100
def farm(storage_limit):
global storage
while storage < storage_limit:
storage += 1
#time.sleep(1)
print(storage)
return storage
farm(storage_limit)
print(storage)
Here is another way. It changes farm()
to return the modified value of its local variable storage
, and it changes the call to farm()
to update the different variable at outer scope named storage
based on this return value.
import time
storage = 0
storage_limit = 100
def farm(storage, storage_limit):
while storage < storage_limit:
storage += 1
#time.sleep(1)
print(storage)
return storage
storage = farm(storage, storage_limit)
print(storage)