Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
99 views
in Technique[技术] by (71.8m points)

python - Print not showing string variables

so, me and one of my friends are making a Percy Jackson text adventure in Python (we are nerds), and I was trying to print " Well [PlayerName], this is Camp Half-Blood", but the result is: " Well , this is Camp Half-Blood!". Does anyone know what I'm doing wrong? (The problem bit is at the bottom) (Please don't make fun of our group name)

import time
import random
import pickle
import os



answer_A = ['A', 'a']
answer_B = ['B', 'b']
answer_C = ['C', 'c']
yes = ['y', 'Y']
no = ['n', 'N']
save = ['save']

room = 0
name = ""

def menu():
    print("FireFluxGames presents...

")
    time.sleep(0.5)
    print("Percy Jackson")
    time.sleep(0.1)
    print("-------------")
    time.sleep(0.1)
    print("Text Adventure

")
    time.sleep(0.1)
    print("Options:
")
    print("A. New Game
"
          "B. Continue from last save
"
          "C. View Credits
")

    menuinput = input(">>> ")
    if menuinput in answer_A:
        start()

    elif menuinput in answer_B:
        load()

    elif menuinput in answer_C:
        credits()


def save():
    with open('save.pckl', 'wb') as f:
        pickle.dump(room, f)
        pickle.dump(name, f)

def load():
    with open('save.pckl', 'rb') as f:
        room = pickle.load(f)
        name = pickle.load(f)
    print(room)

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()

def CHBRoom1():
    print("<Percy> Well {}, this is Camp Half-Blood!".format(name))
        

menu()

question from:https://stackoverflow.com/questions/65936412/print-not-showing-string-variables

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...