缺少1个必需的位置参数



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

import socket
import uuid
import os
import re
HNAME=1
IP=2
MAC=3
ARP=4
ROUT=5
QUIT=6

def get_user_choice(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: "))
print()
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 tablen: ")
table=os.popen('route table')
print(table)
def main(choice):
counter=False
while counter==False:
get_user_choice()
choicefun()
if choice == 6:
counter==True
elif choice == 1:
get_hostname()
elif choice == 2:
get_ipaddr()
elif choice == 3:
get_mac() 
elif choice== 4:
get_arp()
elif choice == 5:
__pyroute2_get_host_by_ip()
main()

发生这种情况是因为调用main函数时没有相应的choice参数:

def main(choice):
...
main()

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

def main():
counter=False
while counter==False:
choice = get_user_choice()
...

但是,get_user_choice函数也有一个choice参数。由于此参数被choice = int(input("Enter your choice: "))覆盖,您可能希望将函数定义为:

def get_user_choice():
...

这部灵魂之作:

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 tablen: ")
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()

你的问题是你没有传递所需的参数来暗示函数。

问题很简单:当您将主函数定义为需要它时,您可以在没有参数的情况下调用它。

def main(choice):
...
main() #<-here

您需要传递一个值作为初始选择,或者将其从main定义中删除

选项1称之为

main(1)

选项2

def main():
...

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

get_user_choice(choice)

您错过了作为arg 通过选择

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

def get_user_choice(choice):
print("Network Toolkit Menu")

选项未设置,因此无法传递尚未创建的参数
当您创建像您这样的脚本时,Python是一种过程化和动态编程语言(即使对此有很多争论,也要接受它(,它会从第一行到最后一行执行您的代码。这将符合的要求

  1. get_user_choice
  2. choicefun
  3. 获取主机名
  4. get_ipadder
  5. get_mac
  6. get_arp
  7. __pyrotee2_get_host_by_ip
  8. def-main(选项(:
  9. main(选项(

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: "))
print("Your Choice is {0}".format(choice))
return choice
def choicefun(choice):
while choice > QUIT or choice < HNAME:
choice = int(input("Please enter a valid number: "))
print()#Wtf?
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 tablen: ")
table=os.popen('route table')
print(table)

def main():
counter=False
#while !counter :
while counter==False:
choice = get_user_choice()
choicefun(choice)#you forgot to pass choice parameter
if choice == 6:
counter==True
elif choice == 1:
get_hostname()
elif choice == 2:
get_ipaddr()
elif choice == 3:
get_mac()#you need to return something and save it in a variable
elif choice== 4:
get_arp()#same
elif choice == 5:
__pyroute2_get_host_by_ip(#??)#you need to pass the parameter ip
if __name__ == 'main':
main()

我修正了一点:如果你刚开始,没有太多的经验,记得写一个函数的输入、输出和目的。第2点,如果你调用一个函数;得到";以功能的名义,你必须做variableOfReturn = getMyfunction(parameter1,paramater2...)
第3点,始终检查函数的声明,如果它需要参数,则必须通过两种方式传递:使用已设置或静态设置的变量如CCD_ 14
不需要创建类,如果您想要一个调用菜单等其他函数的中心函数,请使用if __name__ == '__main__' :

编码101的要点严重不足,请小心,最好尝试一下codechef或其他系统。

相关内容

  • 没有找到相关文章

最新更新