pythonpython-3.xfunctionglobal-variablesmud

How do I use user input, obtained through a function, to alter global variables?


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.

Solution

  • 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 >>>