Wolfsurge
Wolfsurge

Reputation: 37

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...\n\n")
    time.sleep(0.5)
    print("Percy Jackson")
    time.sleep(0.1)
    print("-------------")
    time.sleep(0.1)
    print("Text Adventure\n\n")
    time.sleep(0.1)
    print("Options:\n")
    print("A. New Game\n"
          "B. Continue from last save\n"
          "C. View Credits\n")

    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.\n"
          "You look around, and discover that you are near an\n"
          "archway, with the words: Camp Half-Blood etched into\n"
          "them. You look into the archway, and see a tall, lanky\n"
          "teenager with jet black hair walking towards you,\n"
          "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()

Upvotes: 0

Views: 66

Answers (4)

Lucas Pessoa
Lucas Pessoa

Reputation: 1

The issue is to do with scope. The name variable is defined inside function start and is not available elsewhere, so CHBRoom1 cannot acces it. You can solve this by adding the name parameter to CHBRoom1 and passing the now renamed player_name to it inside start. Here's how it would look:

def start():
print("You wake up in a forest. You don't remember much.\n"
      "You look around, and discover that you are near an\n"
      "archway, with the words: Camp Half-Blood etched into\n"
      "them. You look into the archway, and see a tall, lanky\n"
      "teenager with jet black hair walking towards you,\n"
      "looking concerned. He asks you what your name is...")
player_name = input("What is your name? >>> ")
print(player_name)
room = 1
save()
CHBRoom1(player_name)

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

Upvotes: 0

Pablo C
Pablo C

Reputation: 4761

You must define the function with an argument:

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

and call it this way:

CHBRoom1("Pablo")
#<Percy> Well Pablo, this is Camp Half-Blood!

Upvotes: 1

Wouter
Wouter

Reputation: 275

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.\n"
          "You look around, and discover that you are near an\n"
          "archway, with the words: Camp Half-Blood etched into\n"
          "them. You look into the archway, and see a tall, lanky\n"
          "teenager with jet black hair walking towards you,\n"
          "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.\n"
          "You look around, and discover that you are near an\n"
          "archway, with the words: Camp Half-Blood etched into\n"
          "them. You look into the archway, and see a tall, lanky\n"
          "teenager with jet black hair walking towards you,\n"
          "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().

Upvotes: 1

Krishna Chaurasia
Krishna Chaurasia

Reputation: 9572

You are using the global object name in CHBRoom1 and local object in start method.

Ideally, you should pass around the variables and not use global variables. However, you can make the above implementation work by using the global keyword to use the global object name at all places.

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...\n\n")
    time.sleep(0.5)
    print("Percy Jackson")
    time.sleep(0.1)
    print("-------------")
    time.sleep(0.1)
    print("Text Adventure\n\n")
    time.sleep(0.1)
    print("Options:\n")
    print("A. New Game\n"
          "B. Continue from last save\n"
          "C. View Credits\n")

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

    elif menuinput in answer_B:
        load()

    elif menuinput in answer_C:
        credits()


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

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

def start():
    global name
    print("You wake up in a forest. You don't remember much.\n"
          "You look around, and discover that you are near an\n"
          "archway, with the words: Camp Half-Blood etched into\n"
          "them. You look into the archway, and see a tall, lanky\n"
          "teenager with jet black hair walking towards you,\n"
          "looking concerned. He asks you what your name is...")
    name = input("What is your name? >>> ")
    print(name)
    room = 1
    save()
    CHBRoom1()

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

menu()

Upvotes: 0

Related Questions