Python 需要一个包含代码的字符串

Python 需要一个包含代码的字符串,python,Python,所以我想从头开始做一个简单的基于文本的游戏,但我马上遇到了一个问题。我试图为游戏中发生的事件创建一个类,以使编写代码更容易。我的意思是: class event(object): def __init__(self, text, trigger1, trigger2, outcome1, outcome2): print(text) time.sleep(1) choice= input("What will you do?")

所以我想从头开始做一个简单的基于文本的游戏,但我马上遇到了一个问题。我试图为游戏中发生的事件创建一个类,以使编写代码更容易。我的意思是:

class event(object):
    def __init__(self, text, trigger1, trigger2, outcome1, outcome2):
        print(text)
        time.sleep(1)
        choice= input("What will you do?")
        if choice == trigger1:
               (Somehow execute outcome 1)
        if choice == trigger2:
               (Somehow execute outcome 2)

但我不知道如何使结果包含我稍后将编写的代码,因此任何帮助都将不胜感激

python方法是使用带有函数对象的字典:

def outcome1():
    print("Outcome 1")

def outcome2():
    print("Outcome 2")

def event(text, triggers):
    print(text)
    time.sleep(1)
    choice= input("What will you do?")
    triggers[choice]()

event("You can got west or east.", {
    "go west": outcome1,
    "go east": outcome2,
})

pythonic方法是将字典与函数对象一起使用:

def outcome1():
    print("Outcome 1")

def outcome2():
    print("Outcome 2")

def event(text, triggers):
    print(text)
    time.sleep(1)
    choice= input("What will you do?")
    triggers[choice]()

event("You can got west or east.", {
    "go west": outcome1,
    "go east": outcome2,
})

为什么这在init中?您真的要在每个新事件上创建新实例吗?对于你的问题,如果我没有弄错你的问题,那么这就是你可能想要的代码

def outcome1(text):
    print("outcome1: %s" % text)

def outcome2(text):
    print ("outcome2: %s" % text)

trigger_map = {
    trigger1 : outcome1,
    trigger2 : outcome2,
}

class event(object):
    def __init__(self, trigger_map):
        sefl._tmap = trigger_map

    def onEvent (self, text):
        print(text)
        time.sleep(1)
        choice= input("What will you do?")
        return self._tmap [choice] (text)

为什么这在init中?您真的要在每个新事件上创建新实例吗?对于你的问题,如果我没有弄错你的问题,那么这就是你可能想要的代码

def outcome1(text):
    print("outcome1: %s" % text)

def outcome2(text):
    print ("outcome2: %s" % text)

trigger_map = {
    trigger1 : outcome1,
    trigger2 : outcome2,
}

class event(object):
    def __init__(self, trigger_map):
        sefl._tmap = trigger_map

    def onEvent (self, text):
        print(text)
        time.sleep(1)
        choice= input("What will you do?")
        return self._tmap [choice] (text)

我希望我能正确理解你的问题,所以如果这不符合目标,请随时纠正我

创建类事件的新实例时(按照惯例,类的第一个字符大写)。将函数分配给outcome1和outcome2的值,这两个值将根据各自的触发器执行

def outcomeFunction1():
    #Whatever you'd like to do for first outcome
    print("Hello")

def outcomeFunction2():
    #Whatever you'd like to do for second outcome
    print("Bye")

Event('text', 'trigger1', 'trigger2', outcomeFunction1(), outcomeFunction2())
在类定义中编写:

if choice == trigger1:
   outcome1

elif choice == trigger2:
   outcome2

希望这有帮助

我希望我正确地理解了你的问题,如果这不符合目标,请随时纠正我

创建类事件的新实例时(按照惯例,类的第一个字符大写)。将函数分配给outcome1和outcome2的值,这两个值将根据各自的触发器执行

def outcomeFunction1():
    #Whatever you'd like to do for first outcome
    print("Hello")

def outcomeFunction2():
    #Whatever you'd like to do for second outcome
    print("Bye")

Event('text', 'trigger1', 'trigger2', outcomeFunction1(), outcomeFunction2())
在类定义中编写:

if choice == trigger1:
   outcome1

elif choice == trigger2:
   outcome2

希望这有帮助

如果我误解了你的问题,很抱歉,但我要说的是


这里的答案取决于您是否需要为每个事件创建一个新实例。如果这样做,代码可能看起来像这样(正如Jeremy E.指出的,将类的第一个字符大写是个好主意):

我使用
exec
函数来执行结果。我将一些代码移到了不同的函数中,留下
\uuuu init\uuu
函数来保存其他函数的变量。然后,您可以为每个事件创建一个实例,如下所示:

room = Event("You are in a room with exits to the north and east.", "Where will you go?", "north", "east", "print('You went north.')", "print('You went east.'")
注意,我还添加了一个
question
参数,以避免在每个事件上强制执行
您将做什么?
问题


现在,如果您只需要
事件
类的一个实例,那么代码有点不同:

class Event:
    def make_event(self, text, question, trigger1, trigger2, outcome1, outcome2):
        print(text)
        choice = input(question)
        if choice == trigger1:
            exec(outcome1)
        if choice == trigger2:
            exec(outcome2)
您可以看到代码要短得多。要使用此创建事件,您可以执行以下操作(与前面的示例相同):


编辑:您可能希望完全删除类定义,只保留我在上面为您编写的
make\u event
函数



您可以在这些类示例中选择一个,或者使用它们的想法来定制一个。我希望我回答了你的问题。

如果我误解了你的问题,很抱歉,但我来了


这里的答案取决于您是否需要为每个事件创建一个新实例。如果这样做,代码可能看起来像这样(正如Jeremy E.指出的,将类的第一个字符大写是个好主意):

我使用
exec
函数来执行结果。我将一些代码移到了不同的函数中,留下
\uuuu init\uuu
函数来保存其他函数的变量。然后,您可以为每个事件创建一个实例,如下所示:

room = Event("You are in a room with exits to the north and east.", "Where will you go?", "north", "east", "print('You went north.')", "print('You went east.'")
注意,我还添加了一个
question
参数,以避免在每个事件上强制执行
您将做什么?
问题


现在,如果您只需要
事件
类的一个实例,那么代码有点不同:

class Event:
    def make_event(self, text, question, trigger1, trigger2, outcome1, outcome2):
        print(text)
        choice = input(question)
        if choice == trigger1:
            exec(outcome1)
        if choice == trigger2:
            exec(outcome2)
您可以看到代码要短得多。要使用此创建事件,您可以执行以下操作(与前面的示例相同):


编辑:您可能希望完全删除类定义,只保留我在上面为您编写的
make\u event
函数



您可以在这些类示例中选择一个,或者使用它们的想法来定制一个。我希望我回答了你的问题。

你希望
结果1
结果2
是什么?你真的希望
事件
类对象在它们的
\uu init\uuu()
方法中完成所有这一切吗?该方法在创建时被调用?无论如何,您可以使用
eval
exec
在Python中执行字符串,具体取决于字符串中的代码类型(以及表达式或一+语句)。您希望
output 1
output 2
成为什么?您真的希望
event
类对象在其
初始化()中完成所有这些任务吗
方法,该方法在创建时调用?无论如何,您可以使用
eval
exec
在Python中执行字符串,具体取决于它们中的代码类型(以及表达式或一+语句)。