In this code
money = .3
Things = ["Nothing"]
def main():
print "go to buy things"
print "Current Money %s" % (money)
print "Things you have:"
for item in Things:
print item
wait = raw_input()
buythings(money)
def buythings(money):
print "Type Buy to buy things"
buy = raw_input()
if buy == "buy":
money = money - .3
Things.append("Things")
main()
else:
print "you bought nothing"
print ""
main()
Why after buying the things does the money not go down? This has been a problem to me for a while now and I cant seem to understand how the scope works in this situation.
The global variable
money
is shadowed by the function parametermoney
inbuythings(money)
function. You should remove the parameter for it to work:However, A better approach, as alfasin pointed out, would be passing
money
andThings
as parameters to both functions and not usingglobal
keyword at all:Hope this helps.
You can use a global variable in other functions by declaring it as global in each function that assigns to it:
In your case :