如何动态打印存储在dict值中的函数名?

2024-06-28 21:57:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python和一般的代码都是新手。这是一个小项目,我正在努力实现一个模块化的输入系统的一种游戏的想法,我有。只是为了真正的学习。任何帮助都会很感激,即使只是如何表达我所寻找的?你知道吗

我想有网页(如:简介,菜单,统计等)与他们自己的选项分配给一个数字值,自动更新为您输入所需的数字每个。你知道吗

我不能完全围绕我的头,我已经管理到目前为止,花了相当长的时间在教程,书籍,帖子和一些应用程序,试图使这一点,但我觉得现在卡住了。我试过很多不同的方法,这是我最接近我想做的事情。你知道吗

class Displays():
    def __init__(self, name, heading, pageOptions):
        self.name = name
        self.heading = heading
        self.pageOptions = pageOptions

    def printPage(self):
        print('  *** ' + self.heading + ' ***  ')
        print(self.name)
        print(self.pageOptions)     #I would like to replace this with something like the line below                                  
        #print(self.pageOptions().__name__)     



    def pageInput(pageOptions):
        pInput = int(input('Num: '))
        while pInput in pageOptions:
            print(pageOptions[pInput]())
            print(pageOptions[pInput].__name__)
            return pInput
        else:
            print('Invalid')

class Intros(Displays):
    pass


def Hello():
    print('Working hello')

def Bye():
    print('Working Bye')

def Exit():
    print('Working Exit')
    exit()




intro1 = Intros('Start', 'Starting Screen', {1 : Hello, 2 : Bye, 3 : Exit})
intro2 = Intros('Second', 'Second screen', {1 : 'Hey', 2 : 'Boo', 3 : 'Leave'})
intro3 = Intros('Third', 'Third screen', {1 : 'Hi', 2 : 'Good-Bye', 3 : 'Go Away'})

gL = True
while gL:
    Displays.printPage(intro1)
    Displays.pageInput(intro1.pageOptions)

我目前得到的->

 *** Starting Screen ***  
Start
{1: <function Hello at 0x000002D771AEC1E0>, 2: <function Bye at 0x000002D774110730>, 3: <function Exit at 0x000002D7741107B8>}
Num: 1
Working hello
None
Hello
  *** Starting Screen ***  
Start
{1: <function Hello at 0x000002D771AEC1E0>, 2: <function Bye at 0x000002D774110730>, 3: <function Exit at 0x000002D7741107B8>}
Num: 3
Working Exit
>>> 
--------------------------------------------------------------------------

      *** Starting Screen ***  
    Start
    {1: <function Hello at 0x000002603AC9C1E0>, 2: <function Bye at 0x000002603D2C0730>, 3: <function Exit at 0x000002603D2C07B8>}
    Traceback (most recent call last):
      File "C:/Users/Zander/AppData/Local/Programs/Python/Python37/accs.py", line 50, in <module>
        Displays.printPage(intro1)
      File "C:/Users/Zander/AppData/Local/Programs/Python/Python37/accs.py", line 14, in printPage
        print(self.pageOptions().__name__)
    TypeError: 'dict' object is not callable
    >>> 

当用户输入1-3时,我希望它显示名称,运行函数并更新它切换到的选项。这样我就可以创造故事情节和一切,就像我在介绍1中所做的那样。你知道吗


Tags: nameselfhellodefexitfunctionatworking
2条回答

我想你可以用词典来代替那一行。你知道吗

        print({n: f.__name__ for n, f in self.pageOptions.items()})

字典有一个items方法,它将返回一个iterable。该iterable的每次迭代都包含一个类似tuple(<key>, <value>,)。这可以在字典理解中用来构造一个新的dict,内容稍加修改。你知道吗

一种选择是调整页面选项结构以包含名称信息,这样可以更详细地介绍您的函数,并且灵活地独立于函数名称,如下面的代码。你知道吗

此外,不应该用类调用实例方法,尽管可以在python中调用。你知道吗

class Displays():
    def __init__(self, name, heading, pageOptions):
        self.name = name
        self.heading = heading
        self.pageOptions = pageOptions

    def printPage(self):
        print('  *** ' + self.heading + ' ***  ')
        print(self.name)
        for key in self.pageOptions:
            print(' ', key, self.pageOptions[key]['name'])

    def pageInput(self):
        pInput = int(input('Num: '))
        if pInput in self.pageOptions:
            print(self.pageOptions[pInput]['entry']())
        else:
            print('Invalid')


class Intros(Displays):
    pass


def Hello():
    print('Working hello')


def Bye():
    print('Working Bye')


def Exit():
    print('Working Exit')
    exit()


intro1 = Intros('Start', 'Starting Screen',
                {
                    1: {'name': 'Hello', 'entry': Hello},
                    2: {'name': 'Bye', 'entry': Bye},
                    3: {'name': 'Exit', 'entry': Exit}
                })

gL = True
while gL:
    intro1.printPage()
    intro1.pageInput()

相关问题 更多 >