Python 缺少1个必需的位置参数?

Python 缺少1个必需的位置参数?,python,self,Python,Self,我不知道这个代码有什么问题?它总是给我一个错误。我不想创建一个类。 它一直在给我主函数中“缺少1个必需的位置参数'choice'。 有人有什么建议吗? 脚本应该是一个包含所有功能的菜单 连接到主电源。 我试着做elif's,希望能有所帮助。 我可能需要用“自我” 导入套接字 导入uuid 导入操作系统 进口稀土 HNAME=1 IP=2 MAC=3 ARP=4 路由=5 退出=6 def get_user_选项(选项): 打印(“网络工具包菜单”) 打印(“\uuuuuuuuuuuuuuuuuu

我不知道这个代码有什么问题?它总是给我一个错误。我不想创建一个类。 它一直在给我主函数中“缺少1个必需的位置参数'choice'。 有人有什么建议吗? 脚本应该是一个包含所有功能的菜单 连接到主电源。 我试着做elif's,希望能有所帮助。 我可能需要用“自我”

导入套接字
导入uuid
导入操作系统
进口稀土
HNAME=1
IP=2
MAC=3
ARP=4
路由=5
退出=6
def get_user_选项(选项):
打印(“网络工具包菜单”)
打印(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
打印(“1.显示主机名”)
打印(“2.显示IP地址”)
打印(“3.显示MAC地址”)
打印(“4.显示ARP表”)
打印(“5.显示路由表”)
打印(“6.退出”)
打印()
choice=int(输入(“输入您的选择:”)
打印()
返回选择
def choicefun(选项):
当选择>退出或选择>ele)&0xff)
对于(0,8*6,8)][:-1]范围内的元素
def get_arp(行):
打印(“ARP表”)
以os.popen('arp-a')作为f:
data=f.read()
对于re.findall(“([-.0-9]+)\s+([-0-9a-f]{17})\s+(\w+”)中的行,数据:
打印(行)
回程线
通过ip获取主机(ip):
打印(“路由表\n:)
table=os.popen(‘路由表’)
打印(表格)
def主(选项):
计数器=假
当计数器==False时:
获取用户选择()
choicefun()
如果选项==6:
计数器==真
elif选项==1:
获取主机名()
elif选项==2:
获取\u ipaddr()
elif选项==3:
get_mac()
elif选项==4:
获取_arp()
elif选项==5:
__pyroute2通过ip获取主机
main()
您没有通过选择,因为arg

这将起作用:

import socket
import uuid
import os
import re

HNAME=1
IP=2
MAC=3
ARP=4
ROUT=5
QUIT=6



def get_user_choice():
    print("Network Toolkit Menu")
    print("_____________________________________")
    print("1. Display the Host Name")
    print("2. Display the IP Address")
    print("3. Display the MAC Address")
    print("4. Display the ARP Table")
    print("5. Display the Routing Table")
    print("6. Quit")
    print()
    
    choice = int(input("Enter your choice: "))
    
    return choice

def choicefun(choice):
   
    while choice > QUIT or choice < HNAME:
        
        choice = int(input("Please enter a valid number: "))
        print()
        
    return choice

def get_hostname(host):
    host=socket.gethostname()
    print("\n The host name is: ", host)
    #return host

def get_ipaddr(ipv4):
    ipv4=socket.gethostbyname()
    print("\n The IPv4 address of the system is: ", ipv4)
    #return ipv4

def get_mac(ele):
    print ("The MAC address is : ", end="") 
    print (':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff) 
    for ele in range(0,8*6,8)][::-1]))

def get_arp(line):
    print("ARP Table")
    with os.popen('arp -a') as f:
        data=f.read()
    for line in re.findall('([-.0-9]+)\s+([-0-9a-f]{17})\s+(\w+)',data):
        print(line)
    return line

def __pyroute2_get_host_by_ip(ip):
    print("Routing table\n: ")
    table=os.popen('route table')
    print(table)

def main():
    counter=False
    while counter==False:
        choice = get_user_choice()
        choicefun(choice)         
        if str(choice) == "6":            
            counter==True
        elif str(choice) == "1":
            get_hostname()
        elif str(choice) == "2":
            get_ipaddr()
        elif str(choice) == "3":
           get_mac() 
        elif str(choice)== "4":
            get_arp()
        elif str(choice) == "5":
            __pyroute2_get_host_by_ip()

main()
导入套接字
导入uuid
导入操作系统
进口稀土
HNAME=1
IP=2
MAC=3
ARP=4
路由=5
退出=6
def get_user_choice():
打印(“网络工具包菜单”)
打印(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
打印(“1.显示主机名”)
打印(“2.显示IP地址”)
打印(“3.显示MAC地址”)
打印(“4.显示ARP表”)
打印(“5.显示路由表”)
打印(“6.退出”)
打印()
choice=int(输入(“输入您的选择:”)
返回选择
def choicefun(选项):
当选择>退出或选择>ele)&0xff)
对于(0,8*6,8)][:-1]范围内的元素
def get_arp(行):
打印(“ARP表”)
以os.popen('arp-a')作为f:
data=f.read()
对于re.findall(“([-.0-9]+)\s+([-0-9a-f]{17})\s+(\w+”)中的行,数据:
打印(行)
回程线
通过ip获取主机(ip):
打印(“路由表\n:)
table=os.popen(‘路由表’)
打印(表格)
def main():
计数器=假
当计数器==False时:
choice=get\u user\u choice()
choicefun(选择)
如果str(选项)=“6”:
计数器==真
elif str(选项)=“1”:
获取主机名()
elif str(选择)=“2”:
获取\u ipaddr()
elif str(选项)=“3”:
get_mac()
elif str(选项)=“4”:
获取_arp()
elif str(选项)=“5”:
__pyroute2通过ip获取主机
main()

您的问题是没有将所需的参数传递给InInde函数。

出现这种情况是因为您在调用
main
函数时没有相应的
选项
参数:

def main(choice):
    ...
main()
您需要传递一个
choice
参数,或者从函数中删除
choice
参数。似乎
choice
主要由
get\u user\u choice()
定义,在这种情况下,代码可以读取:

def main():
    counter=False
    while counter==False:
        choice = get_user_choice()
...
但是,
get\u user\u choice
函数也有一个
choice
参数。由于此参数被
choice=int(输入(“输入您的选择”)
覆盖,您可能希望将函数定义为:

def get_user_choice():
    ...

问题很简单:当定义主函数时,调用它时没有参数

def main(choice):
    ...

main() #<-here
选择2

def main():
    ...

但这将给您留下第二个问题,在您的主函数中,这就是您永远不会将从
get_user_choice
获取的值赋值或更改到
choice
变量,因此您将根据您如何解决第一个问题而得到第二个错误,或者陷入无限循环,因为该选择不会更改

我认为您应该重新评估您的设计:

def get_user_choice(choice):
print("Network Toolkit Menu")
选项未设置,因此传递未创建的参数没有用。
当您创建像您这样的脚本时,Python是一种过程和动态编程语言(即使对此有很多争论,也接受它),它从第一行到最后一行执行您的代码。 那是正常的

  • 获取用户选择
  • choicefun
  • 获取主机名
  • 获取IP地址
  • 找麦克
  • 获取arp
  • __派克
    main(1)
    
    def main():
        ...
    
    def get_user_choice(choice):
    print("Network Toolkit Menu")