python - Need to have a string contain code -


so i'm trying make simple text based game scratch, , i'm running problem right away. i'm trying create class events happen in-game make writing code easier. here's mean:

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

but don't know how make outcomes contain code write later, appreciated!

the pythonic way use dictionary function objects:

def outcome1():     print("outcome 1")  def outcome2():     print("outcome 2")  def event(text, triggers):     print(text)     time.sleep(1)     choice= input("what do?")     triggers[choice]()  event("you can got west or east.", {     "go west": outcome1,     "go east": outcome2, }) 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -