1

I have a global variable that needs to be altered by user input generated by a function.

I'm trying to make a Zork style text game and want the character name, input by the user during a character creation function, to alter a global variable.

I've been able to create a class to store character information and been able to display most of the information in the class on a mock command prompt I have appear when input options are available to the user.

I use a global variable to define the character's name until the character creation stage. I use the 'global' keyword in the creation() function to alter the 'name' variable with user input.

When the prompt is ready to be used it still only displays the name as 00 instead of the input generated during the creation() function

I am exceedingly novice. Any advice, tips or direction would be cherished.

import time
name = "00" ##this is what we want to change

##
def Intro():
    print("\n\n\nWelcome to the game.")
    time.sleep(1)
    print("This is an attempt to make an interactive text based game.")

##
def Creation():
    Character_name = input("\nWhat will your Character's name be: ")
    time.sleep(1)
    print("\nWelcome to the game " + Character_name + " \n" )
    time.sleep(1.5)
    Character_class = input("In one word, name " + Character_name + "'s profession: ")
    t00n = Character_name + " the " + Character_class
    global name ##here I am using the global keyword
    name = t00n
    time.sleep(1)
    print("\n" + t00n + "\n")
    time.sleep(2)
    next_func = input("When ready type 'next' to begin.\n>>>:")

    if next_func == "next":
        segway()
    else:
        Jump()

##
def Jump():
    Jump_Prompt = input("Just 'Jump' on in\n>>>: ")
    if Jump_Prompt == "Jump":
        segway1()
    else:
        Jump()

##
def segway():
    print("A room with options to choose from")
    prompt()    

class Character:

    def __init__(self, name, HP, full_HP, AtS, AR):
        self.name = name ##should = t00n now?
        self.hp = HP
        self.full_HP = full_HP
        self.AtS = AtS
        self.AR = AR

    def stat_bar(self):
        return '{} {} {} {} {} {}'.format("[Name:]", self.name, "[HP:]", self.hp, "[Max HP:]", self.full_HP)

Player1 = Character(name, 100, 100, 1, 0)

##
def prompt():
    _pr = input("<<< " + Character.stat_bar(Player1) + " >>> \n")
    return _pr


#Begin
Intro()
Creation()
segway()

##The prompt() function still displays the name as 00 even tho the creation() function is using the 'global' keyword to change the 'name' variable to the user input.
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
Marcvs
  • 35
  • 5
  • so in the prompt when someone types his name, in next prompt it should come up e,g `<<< [Name:] Joe [HP:] 100 [Max HP:] 100 >>> ` if Joe was the name typed? – Devesh Kumar Singh May 25 '19 at 03:44
  • Essentially yes. I'd like it to return the `t00n variable` defined in `creation()` The user inputs two pieces of data that that can be returned using the `t00n variable`. I'd like to change the `global name` `variable` to call the `t00n variable` in `creation()` – Marcvs May 25 '19 at 03:55

2 Answers2

0

Ahhh, took a little bit, but I think I found the problem.

You initialize Player 1 using the name variable before calling Creation(), where you change the global name variable, so Player1 is created with the original name “00.”

Move the line:

Player1 = Character(name, 100, 100, 1, 0)

Put it after Creation() at the bottom but before segway()

Python more or less executes any unindented code (code that isn’t in a function, class, etc.) from top to bottom.

So, moving from top to bottom in your program, it sets name to “00”, then creates Player1 with the original name, then calls Intro(), Creation() (which changes the name to t00n), and finally segway().

Daniel C Jacobs
  • 691
  • 8
  • 18
  • I'm not sure I follow. If i move the `Player1 = Character(name, 100, 100, 1, 0)` then I get a **NameError: name 'Player1' is not defined.** – Marcvs May 25 '19 at 03:32
  • Can I see what your code looks like after you’ve moved it? – Daniel C Jacobs May 25 '19 at 03:45
  • I don't know how to show you... It won't let me fit all of it in the comment box. – Marcvs May 25 '19 at 03:49
  • class Character: def __init__(self, name, HP, full_HP, AtS, AR): self.name = name self.hp = HP self.full_HP = full_HP self.AtS = AtS self.AR = AR def stat_bar(self): return '{} {} {} {} {} {}'.format("[Name:]", self.name, "[HP:]", self.hp, "[Max HP:]", self.full_HP) ## def prompt(): _pr = input("<<< " + Character.stat_bar(Player1) + " >>> \n") return _pr #Begin Intro() Creation() Player1 = Character(name, 100, 100, 1, 0) segway() – Marcvs May 25 '19 at 03:50
  • See what happens if you put `global Player1` inside the stat_bar function – Daniel C Jacobs May 25 '19 at 04:09
0

You need to use the global keyword in your prompt, and update Player1.name with that global name

def prompt():

    #Take name from global scope
    global name

    #Assign it to Player1 name
    Player1.name = name

    _pr = input("<<< " + Character.stat_bar(Player1) + " >>> \n")
    return _pr

Then your prompt will work as intended, for example

Welcome to the game.
This is an attempt to make an interactive text based game.

What will your Character's name be: Joe

Welcome to the game Joe 

In one word, name Joe's profession: Don

Joe the Don

When ready type 'next' to begin.
>>>:next
A room with options to choose from
<<< [Name:] Joe the Don [HP:] 100 [Max HP:] 100 >>> 
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40