【问题标题】:How do you run a user defined function [duplicate]你如何运行用户定义的函数[重复]
【发布时间】:2018-11-09 19:51:06
【问题描述】:

我正在尝试编写一个脚本来教我的朋友我掌握的一点 Python 知识,并刷新我自己的记忆。我刚开始,它也没有完成,但是当我试运行它时,什么也没有发生。如何运行函数?

这是我目前的代码:

def mainmenu():
    choice = {
        'print',
        'comments',
        'if',
        'else',
        'and',
        'or',
        'elif',
        'input',
        'variables',
        'strings',
        'def',
        'class',
        'modules',
        }
    print("What would you like to learn about?")
    for statement in choice():
        print("- Type {} to learn about {}".format(statement))
    pclass = input(">>>")
    if pclass in choice():
        return choice[pclass]()
    else:
    print("Invalid Choice")

另外,在我的 for 选择语句中,我试图让它为集合中的每个字符串重复打印内容;我知道声明可能甚至不在我需要输入的范围内。关于我应该用什么替换它的任何想法?

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    该脚本有一些缺陷(见下文),您可以通过在使用开闭括号定义函数之后调用 Python 中的函数(如果没有传递参数)。一些工作代码将是:

    def mainmenu():
    choice = {
        'print',
        'comments',
        'if',
        'else',
        'and',
        'or',
        'elif',
        'input',
        'variables',
        'strings',
        'def',
        'class',
        'modules',
        }
    print("What would you like to learn about?")
    for statement in choice:
        print("- Type {} to learn about {}".format(statement, statement))
    pclass = input(">>>")
    if pclass in choice:
        return pclass
    else:
        print("Invalid Choice")
    
    mainmenu()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-04
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      相关资源
      最近更新 更多