1

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

我正在尝试制作一个Zork 风格的文本游戏,并希望input用户在创建角色期间更改角色名称function更改全局变量

我已经能够创建一个来存储字符信息,并且能够在用户可以使用选项时出现的模拟命令提示符上class显示大部分信息。classinput

在角色创建阶段之前,我使用 aglobal variable来定义角色的名称。我使用 ' global' 关键字来改变usercreation() function的 'name' 。variableinput

当提示准备好使用时,它仍然只显示名称00而不是inputcreation() 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.
4

2 回答 2

0

啊,花了一点时间,但我想我发现了问题。

在调用之前使用 name 变量初始化 Player 1 Creation(),在其中更改全局 name 变量,因此使用原始名称“00”创建 Player1。</p>

移动线:

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

把它放在Creation()底部之后但之前segway()

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

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

于 2019-05-25T03:22:23.923 回答
0

您需要在您的 , 中使用 global 关键字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 >>> 
于 2019-05-25T04:07:25.090 回答