Python:重复导入外部文件

时间:2014-09-20 13:51:29

标签: python python-3.x

我用python制作了一个命令类程序,我还制作了一个外部计算器应用程序。问题是,如果我首先输入该命令,我只能启动计算器,如果我之前输入了另一个命令或者如果我尝试重新打开应用程序,它将不会打开。我使用的是python 3.4.1。这是我的代码:

    # This is a project by zak and shuaib.

    # Startup Words:
    print ("----------[START PAGE]----------")

    print ("Welcome to zak and shuaib's pre-alpha stage command prompt!")
    print ("Type 'helpone' for a list of commands!!!")
    import sys
    # This line of code makes all of the commands work!

    command=input(">")

    # Reset code:



    # This shows the 'helpone' command:

    helpone=" cal=calculator app."

    if command == "helpone":
     print (helpone)


    # This shows the 'helptwo' command:

    helptwo= "Type 'new' for added improvements. For your info, the old bugs have been fixed."

    if command=="helptwo":
        print (helptwo)


    # This shows the 'new' command:

    new=("New stuff: the 'new' command, the 'ver' command and the 'cal' command! Also, the code                   is less messy because of the addition of 'import' code.")

    if command == "new":
     print (new)
     print ("Also, comming soon is the time command which will tell you the time and a cool app                                  that tell you how long you have lived! These apps have already been made, just some compatibility         issues we should fix soon!")
    # Here is the 'ver' command:

    ver= ("zak's and shuaib's command prompt version: v0.1.4.")

    if command == "ver":
     print (ver)

    # This shows all of the app commands:

    # Here is the calculator app:

    if command == "cal":
     import Calculator

1 个答案:

答案 0 :(得分:1)

我无法在命令提示程序中看到循环,因此无论用户在>提示符处输入哪些命令,我​​都无法理解它如何处理多个命令。 / p>

您需要了解有关编写结构合理的Python的更多信息:您应该将代码放入函数中;这将让你更好地控制它的运行方式。

此外,缩进在Python中非常非常重要。建议每个级别的缩进使用4个空格。不一致的缩进使代码难以阅读,并可能导致错误或语法错误。

我怀疑您的Calculator.py程序的编写方式与上面的代码类似。这就是当命令提示程序执行import Calculator语句时计算器运行的原因。它确实不应该那样做。

在结构合理的Python中,import语句通常写在文件顶部附近。导入Python模块时,其函数和常量可用于导入它的程序;它可以做一些初始化,但它应该只是开始无所事事。

因此,更改Calculator.py,使其包含函数定义,如:

Calculator.py

def calculator():
    #Put your calculator code here
    #....

#At the end of the file...
def main():
    calculator() 

if __name__ == '__main__':
    main()

这样,您仍然可以使用

运行计算器

python Calculator.py

将其导入zakacmd.py时行为正确

同样,您的命令提示符程序应该类似于:

zakacmd.py

import sys
import readline
import Calculator

# This is a project by zak and shuaib. With help from PM 2Ring. :)

def my_commands():
    print("----------[START PAGE]----------")
    print("Welcome to zak and shuaib's pre-alpha stage command prompt!")
    print("Type 'helpone' for a list of commands!!!")

    while True:
        command = input("> ")

        # Reset code:
        if command == "quit":
            break

        helpone = "helpone = This help message\nhelptwo = additional help\nnew = recently added commands\nver = version\ncal = calculator app\nquit = exit back to the OS prompt"
        if command == "helpone":
            print(helpone)

        helptwo= "Type 'new' for added improvements. For your info, the old bugs have been fixed."
        if command == "helptwo":
            print(helptwo)

        new = "New stuff: the 'new' command, the 'ver' command and the 'cal' command! Also, the code is less messy because of the addition of 'import' code."
        if command == "new":
            print(new)
            print("Also, coming soon is the time command which will tell you the time and a cool app that tell you how long you have lived!\nThese apps have already been made, just some compatibility issues we should fix soon!")

        ver= ("zak's and shuaib's command prompt version: v0.1.5.")
        if command == "ver":
            print(ver)

        # Here is the calculator app:
        if command == "cal":
            print('Loading calculator...')
            Calculator.calculator()

    print("Bye!")


def main():
    my_commands() 


if __name__ == '__main__':
    main()

现在,您的所有命令都很好地打包在一个函数my_commands()中。并且它们处于while循环中,因此用户可以选择多个命令。

lot 可以对此代码进行更多改进(例如,将命令提示放入listdict),但希望 这会让你朝着正确的方向前进。

玩得开心!