如何让我的程序成为主要功能?

时间:2015-04-26 20:24:13

标签: python function input module main

我希望我的程序有一个main函数,所以我可以将它作为模块或主程序运行。主要功能对我来说是新的,我不知道如何做到这一点。

#!的/ usr / bin中/ python3

def main():
#left dashboard
    ld = raw_input("Left dashboard switch (0 or 1): ")

#right dashboard
    rd = raw_input("Right dasboard switch (0 or 1): ")

#child lock
    cl = raw_input("Child lock switch (0 or 1): ")

#master lock
    ml = raw_input("Master unlock switch (0 or 1): ")

#left inside
    li = raw_input("Left inside handle (0 or 1): ")

#left outside
    lo = raw_input("Left outside handle (0 or 1): ")

#right inside
    ri = raw_input("Right inside handle (0 or 1): ")

#right outside
    ro = raw_input("Right outside handle (0 or 1): ")

#gear shift
    gs = raw_input("Gear shift position (P, N, D , 1 ,2 ,3 or R): ")

    d = 0
    r = 0
    l = 0
    if gs == "P" and ml == "1" and cl == "0":
            d = 1
    if ri == "1" or ro == "1" or rd == "1":
            r = 1
    if li == "1" or lo == "1" or ld == "1":
            l = 1
    if d == 0:
            print "Both doors closed"
if _name_ == "_main_":
    main()

1 个答案:

答案 0 :(得分:2)

def main():
    #code here

if __name__ == '__main__': main()

所有代码都需要包含在main函数中。该函数在底部调用 python docs - __main__。 有关主here

的更多信息