如何使用通过函数获得的用户输入来更改全局变量?

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

我有一个全局变量,需要通过函数生成的用户输入进行更改。

我正在尝试制作一个 Zork 风格的文字游戏 并且想要角色名称,input 由用户在角色创建期间 function改变一个全局变量

我已经能够创建一个 class 来存储角色信息,并且能够在 class 中显示我在 [=11] 时出现的模拟命令提示符中的大部分信息=] 选项可供用户使用。

我用一个global variable来定义角色的名字,直到角色创建阶段。我在 creation() function 中使用 'global' 关键字来改变用户 input.

的 'name' variable

当提示准备好使用时,它仍然只将 名称显示为 00 而不是在 [=18= 期间生成的 input ] function

我是新手。任何建议、提示或指导都将受到重视。

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.

啊啊啊啊啊啊啊啊啊啊啊啊啊啊,不过我好像找到问题了

您在调用 Creation() 之前使用 name 变量初始化了 Player 1,您在此处更改了全局 name 变量,因此 Player1 以原始名称“00”创建。

移行:

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

放在底部 Creation() 之后 segway()

之前

Python 或多或少地从上到下执行任何未缩进的代码(不在函数中的代码,class 等)。

因此,在您的程序中从上到下移动,它将名称设置为“00”,然后使用原始名称创建 Player1,然后调用 Intro()Creation()(这会更改名称到 t00n),最后 segway().

您需要在 prompt 中使用全局关键字,并使用该全局名称更新 Player1.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

然后您的提示将按预期工作,例如

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