如何在Python中的函数之间传递变量

如何在Python中的函数之间传递变量,python,python-2.7,Python,Python 2.7,很抱歉代码太长,但我觉得重要的是要包含我试图完成的内容。我是Python和编程的初学者,我试图制作一个简单的基于文本的冒险游戏。游戏一开始运行得很好,直到我添加了与蜜蜂的遭遇。我运行了这个程序,我选择从熊身上运行,所以我的hp应该是40,显示出来了。然而,当我选择打蜜蜂时,我的生命值应该是0,因为40(我当前的生命值)-40=0。然而,我的生命值显示为60,就好像熊遭遇从未发生过一样。有什么方法可以解决这个问题吗?或者这是Python中的一个限制 from sys import exit fro

很抱歉代码太长,但我觉得重要的是要包含我试图完成的内容。我是Python和编程的初学者,我试图制作一个简单的基于文本的冒险游戏。游戏一开始运行得很好,直到我添加了与蜜蜂的遭遇。我运行了这个程序,我选择从熊身上运行,所以我的hp应该是40,显示出来了。然而,当我选择打蜜蜂时,我的生命值应该是0,因为40(我当前的生命值)-40=0。然而,我的生命值显示为60,就好像熊遭遇从未发生过一样。有什么方法可以解决这个问题吗?或者这是Python中的一个限制

from sys import exit
from time import sleep
import time

#Hp at start of game:
hp = 100

#The prompt for inputs
prompt = "> "

#Bear encounter
def bear(hp):
    choice = raw_input("> ")
    if "stand" in choice:
        print "The bear walks off, and you continue on your way"
    elif "run" in choice:
        print "..."
        time.sleep(2)
        print "The bear chases you and your face gets mauled."
        print "You barely make it out alive, however you have sustained serious damage"
        hp = hp-60
        currenthp(hp)
    elif "agressive" in choice:
        print "..."
        time.sleep(2)
        print "The bear sees you as a threat and attacks you."
        print "The bear nearly kills you and you are almost dead"
        hp = hp-90
        currenthp(hp)
    else:
        print "Well do something!"
        bear(hp)

#Bee encounter
def bee(hp):
    choice = raw_input(prompt)
    if "run" in choice:
        print "..."
        sleep(2)
        print "The bee flies away and you continue on your way."
        currenthp(hp)
    elif "swat" in choice:
        print "..."
        sleep(1)
        print "You succesfully kill the bee. Good Job!"
        sleep(1)
        print "Wait a minute"
        sleep(2)
        print "The bee you killed gives off pheremones, now there are hundreds of bees chasing you."
        print "The bees do some serious damage."
        hp = hp-40
        sleep(1)
        currenthp(hp)
    else:
        print "Well, do something."
        bee(hp)

#Function to display the current hp of the current player
def currenthp(hp):
    if hp < 100:
        print "Your hp is now at %d" % hp
    elif hp <= 0:
        dead()
    else:
        print "You are still healthy, good job!"

#Called when player dies
def dead():
    print "You sustained too much damage, and as a result have died."
    time.sleep(3)
    print "GAME OVER!"
    print "Would you like to play again?"
    choice = raw_input("> ")
    if "y" in choice:
        start_game()
    else:
        exit(0)

#Called to Start the Game, useful for restarting the program       
def start_game():
    print "Welcome to Survival 101"

#START OF GAME
start_game()
print "You start your regular trail."
print "It will be just a little different this time though ;)"

time.sleep(3)
print "You are walking along when suddenly."
time.sleep(1)
print "..."
time.sleep(2)

#Start of first encounter
print "Wild bear appears!."
print "What do you do?"
print "Stand your ground, Run away, be agressive in an attempt to scare the bear"

#first encounter
bear(hp)

#Start of second encounter
print "You continue walking and see a killer bee approaching you"
print "What do you do"
print "run away, swat the bee away"
bee(hp)
从系统导入退出
从时间上导入睡眠
导入时间
#游戏开始时的Hp:
马力=100
#输入提示
prompt=“>”
#遭遇熊
def熊(hp):
选择=原始输入(“>”)
如果选择“站立”:
打印“熊走开了,你继续前进”
elif选项中的“运行”:
打印“…”
时间。睡眠(2)
打印“熊追赶你,你的脸被打伤。”
打印“你几乎没有活着出来,但是你受到了严重的伤害”
hp=hp-60
当前hp(hp)
选择中的elif“agressive”:
打印“…”
时间。睡眠(2)
打印“熊视你为威胁并攻击你。”
打印“熊差点杀了你,你差点死了”
hp=hp-90
当前hp(hp)
其他:
打印“好的,做点什么!”
熊(hp)
#蜜蜂相遇
def bee(hp):
选项=原始输入(提示)
如果选择“运行”:
打印“…”
睡眠(2)
打印“蜜蜂飞走了,你继续前进。”
当前hp(hp)
elif“swat”选项:
打印“…”
睡眠(1)
打印“你成功地杀死了蜜蜂,干得好!”
睡眠(1)
打印“等一下”
睡眠(2)
打印“你杀死的蜜蜂会释放费利蒙,现在有数百只蜜蜂在追你。”
打印“蜜蜂造成了严重的伤害。”
hp=hp-40
睡眠(1)
当前hp(hp)
其他:
打印“好吧,做点什么。”
蜜蜂(hp)
#功能显示当前播放机的当前hp
def当前hp(hp):
如果hp<100:
打印“您的hp现在为%d”%hp

elif hp您将
hp
传递给函数,并在函数内部更新它,但您不会从函数返回更新值
hp
。您应该在函数中指定
return hp
,以返回更新后的值,并且您可以在函数调用中存储(或更新)更新后的值-例如,
hp=bear(hp)

使用return将变量传递给被调用方。。。对于一个孩子来说,这看起来是一个非常酷的项目。你有兴趣教我12岁的孩子写这样的游戏吗?(我知道这个话题,但伯纳德的网页上没有联系方式)。我实际上只是一名8年级学生,但正在努力学习编码。我想在将来成为一名游戏程序员。谢谢你的快速回复,我会试试这是正确的。为什么这么多人制作Python文本冒险游戏?@Elric,因为他们对它们感兴趣,或者喜欢它们,或者认为它们很有趣??