简体   繁体   中英

passing variables between functions - python

from sys import exit


def start():

    name = raw_input("Players name?\n>> ")
#age is the variable to be passed

    age = raw_input("Players age?\n>> ")
    print "Greetings %s of %sth clan of time" % (name, age) 
    first_room()

def first_room():

    print "\nYou are in a room with a couple of doors.
    print" There is a door to your left and right.
    print "Which door do you take?"""

    first_room = raw_input(">> ")
    if first_room == "left":
        second_room()
    elif first_room == "right":
        third_room()
    else:
        dead("a bear in a near by room explodes and his leg blind sides you.")

def dead(why):

    print why, "Good blooper scene!"
    exit(0)

def second_room():

    print "a npc is here.!"
    print "he's mumbling to himself.\nyou catch a LOTR reference\n"
    print "Do you shout for his attention or flee?"
    next = raw_input(">> ")
    if next == "flee":
        first_room()
    elif next == "shout":
        print "Your shout catches his attention"
        dead("mistakes were made.")
    else:
        dead("a near by bear explosion takes you out.")

def third_room():

    print "There is a sleeping bear here"
    print "You light the wooden sword on fire and use a barrel"
    print "to preform a jump attack with your flaming sword!"
    print "CRITICAL HIT!"
    gold_room()

def gold_room():

    print "\nYou see loot!"
    print "How much do you make off with"
    **loot = raw_input(">> ")**
**#checks age (from start) vs loot**    

    if  loot <= age:
        print "you make away with %s gold!" %(loot)
        exit()
    elif loot > age:
        dead("the bears friend catches up to you. Too heavy to run.")
    else:
        dead("you stumble around until another bear explodes.")

    exit()
start ()

You can define a class that has name and age as attributes. In this class you can define the methods for all the rooms that refer to the age attribute when you need it.

class Game( object ):
    def __init__(self, nameIn, ageIn):
        self.name = nameIn
        self.age = ageIn

    def gold_room(self):
        if loot <= self.age:
            # do something

myGame = Game( 'Peter' , 25 )
myGame.gold_room()

You need your function to take arguments:

def first_room(my_arg):
    print "\nYou are in a room with a couple of doors.
    print" There is a door to your left and right.
    print "Which door do you take?"""
    first_room = raw_input(">> ")
    if first_room == "left":
        second_room()
    elif first_room == "right":
        third_room()
    else:
        dead("a bear in a near by room explodes and his leg blind sides you.")

def start():

    name = raw_input("Players name?\n>> ")
#age is the variable to be passed

    age = raw_input("Players age?\n>> ")
    print "Greetings %s of %sth clan of time" % (name, age) 
    first_room(age) # pass age

You can use the age value passed in using my_arg in the first_room function.

def foo():
    age = 100
    foo1(age)
def foo1(my_arg):
    print (my_arg + 10)


print(foo())
110

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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