How to pass a variable between functions in Python -


sorry long code, felt important include trying accomplish. beginner python , programming in general , trying make simple text-based adventure game. game working @ first until added encounter bees. ran program , chose run bear, hp should @ 40, displayed. however, when chose swat bees, hp should @ 0 because 40(my current hp)-40=0. hp displayed @ 60, if bear encounter never happened. there way can fix or limitation in python?

from sys import exit time import sleep import time  #hp @ start of game: hp = 100  #the prompt inputs prompt = "> "  #bear encounter def bear(hp):     choice = raw_input("> ")     if "stand" in choice:         print "the bear walks off, , continue on way"     elif "run" in choice:         print "..."         time.sleep(2)         print "the bear chases , face gets mauled."         print "you barely make out alive, have sustained serious damage"         hp = hp-60         currenthp(hp)     elif "agressive" in choice:         print "..."         time.sleep(2)         print "the bear sees threat , attacks you."         print "the bear kills , dead"         hp = hp-90         currenthp(hp)     else:         print "well something!"         bear(hp)  #bee encounter def bee(hp):     choice = raw_input(prompt)     if "run" in choice:         print "..."         sleep(2)         print "the bee flies away , continue on way."         currenthp(hp)     elif "swat" in choice:         print "..."         sleep(1)         print "you succesfully kill bee. job!"         sleep(1)         print "wait minute"         sleep(2)         print "the bee killed gives off pheremones, there hundreds of bees chasing you."         print "the bees serious damage."         hp = hp-40         sleep(1)         currenthp(hp)     else:         print "well, something."         bee(hp)  #function display current hp of current player def currenthp(hp):     if hp < 100:         print "your hp @ %d" % hp     elif hp <= 0:         dead()     else:         print "you still healthy, job!"  #called when player dies def dead():     print "you sustained damage, , result have died."     time.sleep(3)     print "game over!"     print "would play again?"     choice = raw_input("> ")     if "y" in choice:         start_game()     else:         exit(0)  #called start game, useful restarting program        def start_game():     print "welcome survival 101"  #start of game start_game() print "you start regular trail." print "it little different time though ;)"  time.sleep(3) print "you walking along when suddenly." time.sleep(1) print "..." time.sleep(2)  #start of first encounter print "wild bear appears!." print "what do?" print "stand ground, run away, agressive in attempt scare bear"  #first encounter bear(hp)  #start of second encounter print "you continue walking , see killer bee approaching you" print "what do" print "run away, swat bee away" bee(hp) 

you pass hp functions , inside function update it, not getting updated value hp function. should specify return(hp) inside function return updated value, , can store (or update) updated value in function call - e.g., hp = bear(hp).


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -