缺少 1 个必需的位置参数?
missing 1 required positional argument?
我不明白这段代码有什么问题?它一直给我一个错误。我不想创建 class。
它一直给我“主函数中缺少 1 个必需的位置参数 'choice'。
有人有什么建议吗?
该脚本应该是一个具有所有功能的菜单
连接到主。
我试着做 elif 的,希望它有帮助。
我可能需要使用“self”
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 table\n: ")
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()
get_user_choice(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 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()
你的问题是你没有将所需的参数传递给函数内部。
发生这种情况是因为您在调用 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():
...
问题很简单:当你定义一个需要它的时候,你调用你的 main 函数时没有参数。
def main(choice):
...
main() #<-here
您需要传递一个值作为您的初始选择,或者将其从 main
定义中删除
选项 1 像这样称呼它
main(1)
选项 2
def main():
...
但这会给你留下第二个问题,在你的主要功能中,那就是你永远不会在这里分配或更改 choice
变量你从 get_user_choice
获得的值所以你会根据你解决第一个问题的方式得到第二个错误,或者因为选择没有改变而陷入无限循环
我认为你应该重新评估你的设计:
def get_user_choice(choice):
print("Network Toolkit Menu")
选择未设置,因此传递尚未创建的参数没有用。
当您创建像您这样的脚本时,Python 是一种过程和动态编程语言(即使对此有很多争论,也请接受它),从第一行到最后一行执行您的代码。
这样就可以了
- get_user_choice
- 选择乐趣
- get_hostname
- get_ipaddr
- get_mac
- get_arp
- __pyroute2_get_host_by_ip
- def 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 table\n: ")
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点,如果你调用一个函数名中有“get”的函数,
你必须做 variableOfReturn = getMyfunction(parameter1,paramater2...)
。
第 3 点,始终检查函数的声明,如果它需要参数,则必须以两种方式传递它:使用已设置或静态的变量
像 choicefun(2)
.
没有必要创建一个 class,如果你想有一个像菜单一样调用其他功能的中心功能,使用 if __name__ == '__main__' :
coding 101 有一些严重缺乏的要领,注意,你最好试试codechef或其他系统。
我不明白这段代码有什么问题?它一直给我一个错误。我不想创建 class。 它一直给我“主函数中缺少 1 个必需的位置参数 'choice'。 有人有什么建议吗? 该脚本应该是一个具有所有功能的菜单 连接到主。 我试着做 elif 的,希望它有帮助。 我可能需要使用“self”
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 table\n: ")
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()
get_user_choice(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 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()
你的问题是你没有将所需的参数传递给函数内部。
发生这种情况是因为您在调用 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():
...
问题很简单:当你定义一个需要它的时候,你调用你的 main 函数时没有参数。
def main(choice):
...
main() #<-here
您需要传递一个值作为您的初始选择,或者将其从 main
定义中删除
选项 1 像这样称呼它
main(1)
选项 2
def main():
...
但这会给你留下第二个问题,在你的主要功能中,那就是你永远不会在这里分配或更改 choice
变量你从 get_user_choice
获得的值所以你会根据你解决第一个问题的方式得到第二个错误,或者因为选择没有改变而陷入无限循环
我认为你应该重新评估你的设计:
def get_user_choice(choice):
print("Network Toolkit Menu")
选择未设置,因此传递尚未创建的参数没有用。
当您创建像您这样的脚本时,Python 是一种过程和动态编程语言(即使对此有很多争论,也请接受它),从第一行到最后一行执行您的代码。
这样就可以了
- get_user_choice
- 选择乐趣
- get_hostname
- get_ipaddr
- get_mac
- get_arp
- __pyroute2_get_host_by_ip
- def 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 table\n: ")
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点,如果你调用一个函数名中有“get”的函数,
你必须做 variableOfReturn = getMyfunction(parameter1,paramater2...)
。
第 3 点,始终检查函数的声明,如果它需要参数,则必须以两种方式传递它:使用已设置或静态的变量
像 choicefun(2)
.
没有必要创建一个 class,如果你想有一个像菜单一样调用其他功能的中心功能,使用 if __name__ == '__main__' :
coding 101 有一些严重缺乏的要领,注意,你最好试试codechef或其他系统。