无法将变量重用于多个独立函数

时间:2015-08-11 15:43:26

标签: python

我试图编写一个通用的文本冒险游戏,以便在Python中变得更好,但我在重用变量方面遇到了麻烦。我将拥有其他功能,可以通过名字来调用角色,而且我不确定如何让它发挥作用。我试着制作" name"全局变量,但错误。到目前为止,这是我的代码:

def name_grab():
    name = raw_input("What is your name? ")
    print name

name_grab()

# it begins!

def start_quest(name):
    print name  
    print "You find yourself washed up on a beach."
    print "Your ship was sunk by mercenaries during a violent storm on the ocean."
    print "You are coverred in seaweed and you keep coughing up salty ocean water."
    print "All you seem to have on you is your gun which was ruined by the water."
    print "You need to find a new weapon and find out where you are."

start_quest()

1 个答案:

答案 0 :(得分:1)

def name_grab():
    name = raw_input("What is your name? ")
    return name

# it begins!

def start_quest(name):
    print name  
    print "You find yourself washed up on a beach."
    print "Your ship was sunk by mercenaries during a violent storm on the ocean."
    print "You are coverred in seaweed and you keep coughing up salty ocean water."
    print "All you seem to have on you is your gun which was ruined by the water."
    print "You need to find a new weapon and find out where you are."

name = name_grab()

start_quest(name)