如何让我的程序成为主要功能?
How to I make my program a main function?
我希望我的程序有一个 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()
def main():
#code here
if __name__ == '__main__': main()
您的所有代码都需要包含在主函数中。该功能是在底部调用的
python docs - __main__
。
有关主要 here
的更多信息
我希望我的程序有一个 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()
def main():
#code here
if __name__ == '__main__': main()
您的所有代码都需要包含在主函数中。该功能是在底部调用的
python docs - __main__
。
有关主要 here