简体   繁体   English

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

[英]How to pass a variable between functions in Python

Sorry for the long code, but I felt that it was important that I include what I was trying to accomplish. 很抱歉,冗长的代码,但是我觉得重要的是要包括我要完成的工作。 I am a beginner with Python and programming in general and I was trying to make a simple text-based adventure game. 我是Python和程序设计的初学者,我试图制作一个简单的基于文本的冒险游戏。 The game was working good at first until I added the encounter with the bees. 最初我的游戏运行良好,直到我添加了与蜜蜂的相遇。 I ran the program and I chose to run from the bear, so my hp should be at 40, which was displayed. 我运行了程序,然后选择从熊运行,因此我的马力应该为40,这已显示出来。 However, when I chose to swat the bees, my hp should then be at 0 because 40(my current hp)-40=0. 但是,当我选择拍打蜜蜂时,我的马力应该为0,因为40(我当前的马力)-40 = 0。 My hp is however is displayed at 60, as if the bear encounter never happened. 但是,我的马力显示为60,好像从未遇到过熊。 Is there some way I can fix this or is this a limitation in Python? 有什么办法可以解决这个问题,或者这是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)

You pass hp to functions and inside a function you are updating it, but you are not getting the updated value hp back from the function. 您将hp传递给函数,并在函数内部对其进行更新,但是没有从函数中获取更新后的hp值。 You should specify return hp inside the function to return the updated value, and you can store (or update) the updated value in the function call - eg, hp = bear(hp) . 您应该在函数内部指定return hp以返回更新的值,并且可以在函数调用中存储(或更新)更新的值-例如, hp = bear(hp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM