为什么这个代码没有执行?

2024-10-01 09:32:14 发布

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

找不到语法错误或编译错误。为什么这个程序不能执行?如果没有编译错误,我无法理解为什么这个程序没有运行。下面的代码在逻辑上会出什么问题? 我可以在这个代码中添加什么使它运行和交互?你知道吗

def main():
    print "Checking platform...\r\r\r"
    platform = systemdetails()

def systemdetails():
    print "Hello! Welcome to the auto-commander"
    print "Please enter the platform specific number to automate."
    platforminput = integer(input ("1. Cisco       2. Linux/Unix     3. Juniper       4. VMware vSphere/NSX \n:"))
    if platforminput ==1:
        platform='cisco_ios'
    elif platforminput ==2:
        platform='linux'
    elif platforminput ==3:
        platform='juniper'
    elif platforminput ==4:
        platform='vmware'
    else:
        print "Commander has to repeat the question...\n\n"
        systemdetails()
    return platform

Tags: theto代码程序maindef错误逻辑
2条回答

您需要调用main()函数。你知道吗

要么像这样:

main()

或者像这样:

if __name__ == "__main__":
    main()

在第二个示例中,只有直接运行程序而不是单独导入模块和运行函数时,才会调用main()。这样,您可以导入模块的函数以单独使用,而无需在导入时运行main()。你知道吗

您需要调用main函数。一个最简单的例子仍然重现你的问题是

def main():
    print "Hello World!"

要让它工作,你需要打电话给你的主管

def main():
    print "Hello World!"

main()

一般来说,如果您没有被导入,您只想调用您的main,可以这样做

def main():
    print "Hello World!"

if __name__ == '__main__':
    main()

相关问题 更多 >