将代码与菜单选择结合在一起-python

时间:2018-08-15 20:25:09

标签: python

我正在努力将脚本的集合与菜单选择结合起来,使我可以选择相应的脚本;

Ex-

# import libaries
import socket
import os
import threading
import struct

 # import other programs
import nc

from ctypes import *
from netaddr import IPNetwork, IPAdress

# ask for network infomration
host = raw_input("Enter Your Machine Ip: ")
subnet = raw_input("Enter Subnet of Network: ")

# what would you like run
menu = {}
menu['1'] = "NC"
menu['2'] = "blah"
menu['3'] = "blah

while True:
    options = menu.keys()
    options.sort()
            for entry in options:
                    print entry, menu[entry]

    selection=raw_input("What would you like to do?")
    if selection == '1':
            print "Runing NC..."
            print "Please wait..."
            if __name__ == "__main__':
                    execut main:

截至目前,它在->处出现错误,无法输入选项:

2 个答案:

答案 0 :(得分:1)

Python对缩进敏感。 for entry in options:应该与其上方的行处于同一级别。

例如,如果您使用像Pycharm这样的IDE,它将在您编码时帮助您捕获类似的内容。

答案 1 :(得分:0)

缩进是错误的。 for entry in options行不应缩进:

   options = menu.keys()
   options.sort()
   for entry in options:
       print entry, menu[entry]

还有一个引号缺失:menu['3'] = "blah应该是menu['3'] = "blah"