In start()
you are not updating the global variable name. Instead you are creating a new local name
variable in the scope of start()
. In CHBRoom1()
it's still reading the unmodified global variable name = ''
. To change a global variable in a function you can do something like this:
def start():
global name
print("You wake up in a forest. You don't remember much.
"
"You look around, and discover that you are near an
"
"archway, with the words: Camp Half-Blood etched into
"
"them. You look into the archway, and see a tall, lanky
"
"teenager with jet black hair walking towards you,
"
"looking concerned. He asks you what your name is...")
name = input("What is your name? >>> ")
print(name)
room = 1
save()
CHBRoom1()
Better yet, pass name
to the CHBRoom1()
function like so:
def start():
print("You wake up in a forest. You don't remember much.
"
"You look around, and discover that you are near an
"
"archway, with the words: Camp Half-Blood etched into
"
"them. You look into the archway, and see a tall, lanky
"
"teenager with jet black hair walking towards you,
"
"looking concerned. He asks you what your name is...")
name = input("What is your name? >>> ")
print(name)
room = 1
save()
CHBRoom1(name)
def CHBRoom1(name):
print("<Percy> Well {}, this is Camp Half-Blood!".format(name))
Now you're passing the local name
variable from start()
as a parameter to CHBRoom1()
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…