Variables in Python: Add and change variables -
i making game in python, , want add store.
i want variable. know how add variables, , how change them, not how increase or decrease variable.
i have never used variables before. have read it, don't remember it.
def level1(): os.system('cls') gold = 500 print print 'you have currently', print (gold), print 'gold' time.sleep(3) level2() def level2(): print print 'congratulation! completed quest! received 200 gold.' time.sleep(2) gold =+ 200 print 'you have now', print (gold), print 'gold.' time.sleep(5)
and result is:
you have 500 gold
congratulation! completed quest! received 200 gold. have 200 gold.
i tried gold + 200, gold += 200, , gold =+ 200, last 1 worked.
i tried
print 'you have now' + gold + 'gold'
but didn't work reason. tried
print 'you have now' + (gold) + 'gold'
i not quite sure what's wrong here, , appreciate can get!
thank much.
edit:
i forgot add huge part of question. sorry that!
==================================================================================
in store, sell multiple items different prices. not of items available in beginning of game. therefore want item check how gold user have. if user have under x gold, can't buy item.
if level have reached level 04, specific item unlocked.
it should gold += 200
, not gold =+ 200
.
secondly, seems gold
variable local each function, i.e. assigning gold
500 in level1()
not set value in level2
. need either pass in arguments, or have global.
to pass arguments:
def level1(gold) : # stuff here level2(gold) def level2(gold) : # stuff here # entry point of application if __name__ == "__main__" : # initialize `gold` here gold = 500 level1(gold)
to use global instance:
# global variable gold = 500 def level1() : # specify want use global instance of gold global gold # stuff here def level2() : global gold # stuff here
Comments
Post a Comment